I have two questions related to the freeing up of memory held by a script object that is no longer needed:
(1) I have used the following two methods but am not sure if one of these or some other method is to be preferred:
set myScriptObject to missing value
or:
set myScriptObject to isNothing()
on isNothing()
return
end isNothing
(2) When a script object is thus freed up, are all its properties and contained script objects also freed up, or should they be freed up explicitly before doing so to the script object itself?
The ARC (automatic reference counting) is adding an extra retain to the object. When an object has only 1 retain (for ARC) meaning the memory is no longer in use, the memory will automatically be freed. Nothing much you can do in AppleScript.
So in short it doesn’t matter which solution you’re using. One object will be released (subtracted one retain) and another object will be retained during a set in AppleScript.
Thank you. Setting to missing value seems to be the simpler approach.
Regarding my second question, is the memory used by the script object’s properties and contained script objects also freed up when I set the script object to missing value? I assume it is but just want to be sure that I don’t need to set each property and contained script object to missing value before setting the script object itself to missing value.
Nope you don’t have to, it’s all taken care of by the garbage collector (ARC and GC; ARC is newer technique).
Memory to an executable is given and taken by the kernel. This means that we ask the kernel for a block/chunk of memory were we can store our data in. The kernel returns a pointer back, which contains only a memory address (starting point) to available memory. Then when we change the pointer to point to another piece of memory there is no longer a pointer our allocated memory meaning we have an piece of memory that belongs to nobody and sitting and using memory that we no longer can access (memory leak).
To avoid this there are garbage collectors and what they basically do is running within your executable inside the runtime (that’s why they slow down performance). When memory is allocated they create a second pointer and store them for own use. Then when you say that you no longer need memory or when when there is no other pointer pointing to the memory the memory will be freed and you avoid unused and unaccessible memory (memory leaks).
Everytime you use an set command you will simply change pointers to point to another address. But the garbage collector will kick in when needed. There is also AppleScript code that can’t be ran by the Objective-C runtime and will be handled by the AppleScript interpreter, which contains it’s own garbage collector and handles this by itself.
Because this all happens on lower level (in C, Objective-C runtime is written in C as AppleScript) you don’t have to worry about this and theoretically it’s impossible to create memory leaks with AppleScriptObjC as with AppleScript. When not using a garbage collector will only create huge leaks and the application will drain down your memory. So always use a garbage collector in AppleScript.