Table of Contents

Language - Garbage Collector (GC)

About

Language that have a garbage collector allows you to create as many variable (objects) as you want and you don't have to worry about destroying them. Their runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

How does the garbage collector know that a variable’s storage can be reclaimed?

The basic idea is that every package-level variable, and every local variable of each currently active function, can potentially be the start or root of a path to the variable in question, following pointers and other kinds of references that ultimately lead to the variable. If no such path exists, the variable has become unreachable, so it can no longer affect the rest of the computation.

Example in Go

      
func g() {
    y := new( int)
    *y = 1
}
var global *int 

func f() { 
        var x int  x = 1
        global = &x 
}

Documentation / Reference