Garbage collection and memory management
The PowerBuilder garbage collection mechanism checks memory automatically
for unreferenced and orphaned objects and removes any it finds, thus
taking care of most memory leaks. You can use garbage collection
to destroy objects instead of explicitly destroying them using the DESTROY statement.
This lets you avoid runtime errors that occur when you destroy an object
that was being used by another process or had been passed by reference to
a posted event or function.
A reference to an object is any variable whose value is the
object. When the variable goes out of scope, or when it is assigned
a different value, PowerBuilder removes a reference to the object
and counts the remaining references, and the garbage collection
process destroys the object if no references remain.
Garbage collection occurs:
-
When the
garbage collection interval has been exceeded and the PowerBuilder
application becomes idle and -
When you explicitly call the GarbageCollect function.
When PowerBuilder completes the execution of a system-triggered
event, it makes a garbage collection pass if the set interval between
garbage collection passes has been exceeded. The default interval
is 0.5 seconds. Note that this system-triggered garbage collection
pass only occurs when the PowerBuilder application is idle, therefore
if a long computation or process is in progress when the interval
is exceeded, garbage collection does not occur immediately.
You can force immediate garbage collection by invoking the GarbageCollect function.
When you use dot notation and OLEObjects, temporary variables are created.
These temporary variables are released only during the garbage collection
process. You might want to invoke GarbageCollect inside
a loop that appears to be causing memory leaks.
The garbage collection pass removes any objects and classes
that cannot be referenced, including those containing circular references
(otherwise unreferenced objects that reference each other).
When you post an event or function and pass an object reference,
PowerBuilder adds an internal reference to the object to prevent
its memory from being reclaimed by the garbage collector between
the time of the post and the actual execution of the event or function.
This reference is removed when the event or function is executed.
Exceptions to garbage collection
There are a few objects that are prevented from being collected:
-
Visual objects
Any object that is visible on your screen is not collected because
when the object is created and displayed on your screen, an internal
reference is added to the object. When any visual object is closed, it
is explicitly destroyed. -
Timing objects
Any Timing object that is currently running is not collected
because the Start function for a Timing object
adds an internal reference. The Stop function
removes the reference. -
Shared objects
Registered shared objects are not collected because the SharedObjectRegister function
adds an internal reference. SharedObjectUnregister removes
the internal reference.
Controlling when garbage collection occurs
Garbage collection occurs automatically in
PowerBuilder, but you can use functions to force immediate garbage
collection or to change the interval between reference count checks.
Three functions let you control when garbage collection occurs: GarbageCollect, GarbageCollectGetTimeLimit,
and GarbageCollectSetTimeLimit.
For information about these functions, see
the PowerScript Reference. For an example illustrating
their use, see the Code Examples sample application, described in Chapter 1, “Using
Sample Applications.”
Performance concerns
You can use tracing and profiling to examine the effect of
changing the garbage collection interval on performance.
For information about tracing and profiling,
see the PowerBuilder Users Guide.