Question
· Apr 11

Delete the reference MEMORY

So as asked, I am not talking about KILL - KILL as stated in the docs:


This command removes the variable. If there are no further references to the object, this command also removes the object from memory,

 I want to remove the object from memory even if it is still referenced in memory - I understand that cache is not a low-level programming language, But i was hoping there is a way. 

Notice that have a RegisteredObject and it is a single reference, It is different than the UnSwizzles suggested in other posts.

Product version: Caché 2016.1
Discussion (6)2
Log in or sign up to continue

I'm not sure wha you want to achive, so I ask a puzzling question: do you want to create dungling object? "I want to remove the object from memory even if it is still referenced in memory", as I understand, would free the memory used by an object but let the object referenc(es) intact, so the reference now would point into nirvana. Is that what you want to do? Why? Can you a little bit elaborate, what is your target or the background respectively?

As commented below:

The logic lies in the design of the system i am working on.
Simply put, i would kill a reference to an object only in case i would WANT to aggressively kick any place that tries using this object after i killed it. It is done only on very specific occasion.

So basically it's used as a last resort  because in some places, And although i understand in theory i could add a property and i would turn it off if i want the reference "killed" and just check that property, This is one of the options that came up.

Just for the case, you are lost in the working memory space and desperately searching the spot(s) in your programm where a specific object is once again referenced, here a small handy method which could help you

/// find all variables which contain a given object(reference)
/// 
/// I: the OREF you looking for
/// 
/// O: "" if the spool-device can't be opened
///    [] if no variables contain the given OREF
///    [var1, var2, ... varN] an array of variable names (incl. subscripted and orefs)
///    
ClassMethod FindObject(obj)
{
	set res=[]
	if $d(%)#10,%=obj do res.%Push("%")
	new % set %=obj kill obj
	
	lock +^SPOOL("nextID")			// adapt this lines
	open 2:($o(^SPOOL(""),-1)+1):1	// to your method of
	lock -^SPOOL("nextID")			// creating new spool IDs
	
	if $t {
		use 2
		set spl=$zb
		do $system.OBJ.ShowReferences(.%,1)
		
		for i=1:1:$za-1 {
			set x=$p($zstrip(^SPOOL(spl,i),"<=>w",$c(13,10))," ",3)
			do:x]"%.~" res.%Push(x)
		}
		close 2
		kill ^SPOOL(spl)
		
	} else { set res="" }
	
	quit res
}

Example

USER>kill
USER>set pers=##class(DC.Person).%OpenId(1)
USER>set temp=pers, zz(3)=temp
USER>write ##class(DC.Help).FindObject(pers).%ToJSON()
["pers","temp","zz(3)"]