Activity Forums Salesforce® Discussions Is there any facility of garbage collection in Apex?

  • Gourav

    Member
    June 2, 2016 at 1:02 pm

    Garbage collection is undocumented.

     

    Here's what I think I know:

     

    * Heap size is apparently tracked as a system variable that is adjusted after every line of script.

    * Symbols (i.e. variable names) do not use heap space, only the variables they contain. Null variables, therefore, take no heap space.

    * Governor limits appear to be checked only periodically, so a brief burst over the legal limit is possible, but not advised in any case.

    * Removing an array index, assigning a null value, etc, appears to immedately free heap space.

    * Assigning a new variable, adding an array index, etc, appears to immediately consume heap space.

     

    It's important to note, I guess, that the question of garbage collection is pointless, because either you exceed your limit and you get the boot, or you stay within the limits, and you do not get the boot. Just know that garbage collection works in your favor (i.e. it assumes you're really trying to not exceed any limits), by at least appearing to immediately reclaim free heap space.

     

    I suspect that the heap is periodically defragmented, but there is literally no way to prove or disprove that theory, since memory addresses are never exposed to the developer.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos