What is the Garbage Collector?
The Garbage Collector(GC) is a service that automatically reclaims unused memory.
In the past, allocating and releasing object memory was the program’s responsibility. Unfortunately,
many programs would allocate memory that they later failed to give back to the operating system. Even after the program ended, the memory remained allocated..
So a few Programmers sometimes use process kill to release the memory. haha.
Programmers refer to such memory errors as “memory leaks,” because over time, as such programs execute, the amount of memory available to the system becomes less and less.
In the .NET environment, every object you create resides in the managed heap.
In the following program,
first displays the amount of heap space the program has allocated. The program then allocates memory to store two different objects.
Next, the program discards one of the objects and requests that the garbage collector run and clean up the heap (by passing the value True to the GetTotalMemory method):
To indicate that it will no longer use the TEST2 variable, the program sets the variable to Nothing, which marks the object as discardable for future garbage collection operations. When a program calls the GetTotalMemory method to determine the program’s allocated memory, the program passes a Boolean value that indicates whether the garbage collector should first discard unused objects
before returning the value. If the program passes the value True, the garbage collector will perform a collection and then determine the amount of heap space the program has allocated.
If the program passes the value False, the garbage collector will not discard unused objects and will return the program’s current heap allocation.
source code :
After I compile and execute this program, my screen was display the following output :
The significance that you remember is the .NET heap management software will release the remaining program objects after the program ends.
Have a good night!!