Author Topic: Garbage Collector  (Read 850 times)

Phrog30

  • Guest
Garbage Collector
« on: May 08, 2017, 05:32:37 PM »
Is the frequency at which the GC runs a barometer for good/bad code?  I ask because I was testing an app the other day and it was running quite a bit.  I did a Google and really didn't see anything that stood out to me.

In my case I was reading/writing very quickly to a text file.  Normally, I wouldn't be doing it that quickly.  But, the question popped in my head on what is acceptable, or is this even something to pay attention to?

James

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Garbage Collector
« Reply #1 on: May 08, 2017, 10:04:34 PM »
Not necessarily an indicator of bad code, but probably more of an indicator of code that can be optimized.

The job of the garbage collector is to free up memory that is no longer used so it can be re-used. Every time an object is instantiated using the New keyword, a block of memory is allocated. When there are no more variables referencing that object, it is the job of the GC to find it and release the memory

Consider an example of a timer tick that is setup to occur 50 times per second. In that event handler let's say there is an object that is created with a variable that is local to the event handler subroutine:

Private Sub .........
    Dim MyObject as New Object
End Sub

Since the variable is declared within the subroutine, it's lifetime is over when the sub is exited. This results in the object no longer being referenced and waiting for the GC to find it for releasing the memory. This means every second, there are 50 blocks of memory being allocated, then marked for being released. The GC must keep up with these freeing up these instances that are no longer being used at a rate of 50 per second.

To optimize the code, we can consider keeping the object alive in memory and re-using it instead of creating a new one every time. Modifying the code above:

Private MyObject as Object
Private Sub .........
    if MyObject Is Nothing then
        MyObject = New Object
    end if
End Sub

The variable pointing to the object is now moved out of the sub and becomes a variable with a class level scope. The object instance (block of memory) will no longer lose its reference when the subroutine is exited. So now we allocated the block of memory once and it will exist for the life of the class instead of the life of the subroutine. So now the GC does much less work.

Phrog30

  • Guest
Re: Garbage Collector
« Reply #2 on: May 09, 2017, 07:41:47 AM »
Very nice explanation, thank you.