Advanced PowerBuilder

HomePrevious Lesson: Destroying an Instance
Next Lesson: Polymorphism in PowerBuilder

Garbage Collection

From version 6.0, PowerBuilder automatically removes all unreferenced objects from the memory in its default setting of 0.5 seconds. The garbage collection removes objects when objects goes out of scope�an class variable declared in a script and the script execution completes� and also when another object is assigned to the declared variable. For example:
// No memory is allocated at this time.
uo_xyz luo_1
// uo_xyz is instantiated and memory is allocated.
luo_1 = CREATE uo_xyz
// Other code.
luo_1 = auo_abc
// another object is assigned to luo_1, so 'garbage 
// collection' feature removes uo_xyz from the memory in 
// its next round of collection.

You may have got a question, what if, if I post an event and the current script completes before the posted event starts execution? Good question. PowerBuilder adds an internal reference to all objects that are passed to the posted events and functions, by that those objects will not get removed by the 'garbage collection' feature since they have at least one reference.

There are three exceptions to the garbage collection. The first one is visual objects, garbage collection do not automatically remove visual objects from memory instead they are removed when they are closed. For example, the following code written for the clicked event of a menu item is declaring a local window variable and opening that window. Once the script completes, the reference to the window is gone, so that window should be removed.
w_product_master lw_PrdMstr
OpenSheet( lw_PrdMstr, ParentWindow, 0, Cascaded! )

The second exception is running timing objects. When you start the timing object using Start() function, PowerBuilder adds an internal reference to the timing object.

The third one is shared objects for which PowerBuilder adds an internal reference when you issue SharedObjectRegister() function.

There are three functions available to control garbage collection.

HomePrevious Lesson: Destroying an Instance
Next Lesson: Polymorphism in PowerBuilder