Skip to main content

GC Module

The Garbage Collector (GC) is an important component in many modern programming languages that handle dynamic memory management. The JARU programming language also has its own GC.

The main task of the GC is to free memory that is no longer used in the program. When an object is created in dynamic memory, memory space is allocated for that object. In some programming languages, the programmer has to manually free the memory allocated to the object when it's no longer needed. This can be error-prone and often leads to memory leaks.

In JARU, the GC takes care of automatically freeing memory that is no longer being used. The GC regularly scans memory and looks for objects that are no longer accessible. When an inaccessible object is found, the memory associated with that object is freed.

Technique Used

The technique used by JARU's GC is mark and sweep; this technique marks all objects that are still being used in the program and then eliminates the unmarked objects.

When to Use the GC Module

Although the Garbage Collector is very useful, sometimes it can be inefficient for certain applications due to:

  • Processor overhead: The GC can be an intensive process for the processor
  • Memory fragmentation: The GC may not effectively handle memory fragmentation

In some cases, it may be better for the programmer to handle memory management manually using the GC module commands.

Functions

collect

The collect() function is used to manually run garbage collection, i.e., free memory that is not being used by the program.

use GC

var bytesFreed = GC.collect()
println("Freed ", bytesFreed, " bytes")

The collect() function returns the number of bytes freed during the garbage collection process. This can be useful for monitoring program memory usage.

Note

If the GC is disabled, the collect() function temporarily enables it to perform the collection and then automatically disables it again.

isEnable

The isEnable() function is used to check if the garbage collector is enabled or disabled.

use GC

if (GC.isEnable()) then
println("GC is enabled")
else
println("GC is disabled")
end

Returns true if the garbage collector is enabled and false if it's disabled.

enable

The enable() function is used to enable automatic garbage collector execution.

use GC

GC.enable()

When this function is called, the garbage collector is enabled and set to run automatically in the background.

disable

The disable() function is used to disable automatic garbage collector execution.

use GC

GC.disable()
// Now the programmer must manually free memory

When this function is called, the garbage collector is disabled and the programmer must manually free memory that is no longer being used.

Caution

Disabling the garbage collector can have a negative impact on program performance, as it may consume more memory than necessary. It's recommended to leave automatic garbage collector execution enabled, unless there's a specific reason to disable it.

factor

The factor() function allows you to query or modify the garbage collector's heap growth factor.

Query the current factor

Without arguments, the function returns the current growth factor:

use GC

var currentFactor = GC.factor()
println("Growth factor: ", currentFactor)

Modify the growth factor

With a numeric argument (integer or float), it sets the new growth factor:

use GC

GC.factor(2.0) // Sets the growth factor to 2.0
ParameterTypeDescription
newFactorinteger or floatThe new heap growth factor (optional)
Return ValueDescription
floatThe current growth factor (if no argument is passed)
trueIf the new factor was successfully set
Tip

The growth factor determines how much the heap grows when more memory is needed. A higher factor means fewer collections but more memory usage, while a lower factor means more frequent collections but less memory usage.

Complete Example

use GC

// Show initial state
println("GC enabled: ", GC.isEnable())
println("Growth factor: ", GC.factor())

// Disable GC for intensive operation
GC.disable()

// Perform operations that create many temporary objects
for (var i = 0; i < 1000; i++)
var temp = "Temporary string " + i.toString()
end

// Run manual collection
var freed = GC.collect()
println("Bytes freed: ", freed)

// Re-enable the GC
GC.enable()

// Adjust growth factor for low-memory devices
GC.factor(1.5)