4.10.5 Interaction of Garbage Collection and Heap Expansion

For most programs, the default settings for the garbage collection parameters should suffice. For programs that have high heap requirements, the default parameters may result in a higher ratio of garbage collection time to run time. These programs should be given more space in which to run.

The gc_margin is a non-negative integer specifying the desired margin in kilobytes. For example, the default value of 1000 means that the heap will not be expanded if garbage collection can reclaim at least one megabyte. The advantage of this criterion is that it takes into account both the user's estimate of the heap usage and the effectiveness of garbage collecting.

  1. Setting the gc_margin higher than the default will cause fewer heap expansions and garbage collections. However, it will use more space, and garbage collections will be more time-consuming when they do occur.

    Setting the margin too large will cause the heap to expand so that if it does overflow, the resulting garbage collection will significantly disrupt normal processing. This will be especially so if much of the heap is accessible to future computation.

  2. Setting the gc_margin lower than the default will use less space, and garbage collections will be less time-consuming. However, it will cause more heap expansions and garbage collections.

    Setting the margin too small will cause many garbage collections in a small amount of time, so that the ratio of garbage-collecting time to computation time will be abnormally high.

  3. Setting the margin correctly will cause the heap to expand to a size where expansions and garbage collections are infrequent and garbage collections are not too time-consuming, if they occur at all.

The correct value for the gc_margin is dependent upon many factors. Here is a non-prioritized list of some of them:

The algorithm used when the heap overflows is as follows:

     
     if gc is on
     and the heap is larger than gc_margin kilobytes then
        garbage collect the heap
        if less than gc_margin kilobytes are reclaimed then
           try to expand the heap
        endif
     else
        try to expand the heap
     endif

The user can use the gc_margin option of prolog_flag/3 to reset the gc_margin (see ref-lps-ove). If a garbage collection reclaims at least the gc_margin kilobytes of heap space the heap is not expanded after garbage collection completes. Otherwise, the heap is expanded after garbage collection. This expansion provides space for the future heap usage that will presumably occur. In addition, no garbage collection occurs if the heap is smaller than gc_margin kilobytes.


Send feedback on this subject.