X
10011 Rate this article:
No rating

What the *bleep* is IDL doing? Garbage collection

Anonym

In 8.0, automatic garbage collection was introduced to the IDL language. What it brought was some much needed relief in controlling the memory used by IDL.  Pre IDL 8.0, if you ran:

obj = obj_new('MyIDLObject')

obj = obj_new('MyOtherIDLObject')

Then the first instance of obj was "leaked", meaning the memory could no longer be accessed by IDL and would be inaccessible until IDL was restarted. This is especially bad in large applications where "leaked" memory would slowly consume all the resources of the machine. With the addition of garbage collection, as soon as the second call happens, the first object is freed and the memory it used is released back to IDL. So in IDL 8.4:

IDL> o = obj_new('MyIDLObject') & help,/heap,/brief

Heap Variables:

    # Pointer: 0

    # Object : 1

    # Bytes Heap Memory:  24

IDL> o = obj_new('MyOtherIDLObject') & help,/heap,/brief

Heap Variables:

    # Pointer: 0

    # Object : 1

    # Bytes Heap Memory:  24

However, consider the following example:

        p = plot(/test)

        p = plot(randomu(seed,100))

If you run these statements, both plot windows will stay open even though it seems like IDL's garbage collection should have freed the first instance of p. The reason IDL doesn't free the first instance of p is because of something called reference counting. In order to perform garbage collection, IDL needs to know when the user can no longer reference an object. So if you run:

a = obj_new('MyIDLObject')

b =  a

help,/heap

IDL prints:

Heap Variables:

    # Pointer: 0

    # Object : 1

<ObjHeapVar2>  refcount=2

                STRUCT    = -> MYIDLOBJECT Array[1]

 

The refcount field tells you how many IDL variables are pointing to the same object.  As soon as the refcount becomes 0, IDL's garbage collection will free the object.

Side Note: Be very careful using the OBJ_DESTROY method.  If I call OBJ_DESTROY on b, a will no longer be a valid object.  If you want to free an unnecessary reference to an object, set the variable to either !NULL or 0 to avoid destroying the object for all other references.

Let's get back to the PLOT example. In this case, IDL is holding the refcount above zero since the window is still open. Until you close the window, the plot will stay alive. You may be asking yourself, "well if IDL has the window, how do I get a reference back to it because I really didn't mean to lose my previous reference". Here is where GETWINDOWS() and WINDOW(/current) shine. Let's say you ran the following code:

p = plot(/test)

p = plot(randomu(seed,100))

Oops, we didn't want to lose a reference to the first plot object!  To get that reference back: first, click on the window then run:

w = WINDOW(/current)

Then select the plot inside the window and run:

p1 = w.getselect()

help, p1

P1         PLOT <5168>

The first line snags a reference of the active window. The second line grabs a reference to the plot. Similarly, you can grab a reference to the plot without having to click anything by using GETWINDOWS():

pwin = GETWINDOWS()

Will return an array of references to all active plot windows in the order they were created. By indexing into the desired window, we can grab a reference to the plot by:

tool = pwin[0].gettool()

id = igetid('plot',tool=tool)

obj = tool->getbyidentifier(id)

obj->idlitcomponent::getproperty, _proxy=p1

help, p1

P1         PLOT <5168>

By understanding the lifecycle of IDL objects, you can better manage the memory IDL uses as well as retrieve objects you might have thought were lost.