2011-06-22

Garbage Collection

By default, NINA programs include garbage collection, but the language provides a way to create programs without garbage collection if that is needed. If a program should not include garbage collection, the main-program procedure should include "pragma Garbage_Collection (Enabled => False);" as the first thing in its declarative part. If this is omitted, or specifies "Enabled => True", then the program includes garbage collection.

Modules are often intended to be reusable; such modules should be designed to work correctly whether garbage is collected or not. This means that any module that does dynamic allocation should also do memory management. Allocated memory is released by using the 'Free procedure defined for every reference type:

procedure T'Free (Pointer : in out T);

Since the type of an object is known, one can also write "V'Free" for any object V of a reference type.

When garbage collection is active, 'Free acts as a hint to the garbage collector that the object is probably safe to collect.

Modules that require garbage collection can be marked with pragma Garbage_Collection as the first thing in the module specification, as we did with Unbounded_Stack. Although the pragma name is the same, the pragma parameter has a different name when it appears in a main-program procedure than when it appears in a module: Required for a module and Enabled for a main-program procedure.

No comments:

Post a Comment

Blog Archive