Contents

Garbage Collection

Theory

Garbage Collection is a memory management technique in some programming languages so that developers don’t need to worry about memory deallocation after creating new objects, because garbage collector will destroy those objects automatically. The main purpose of Garbage Collector is to track live object, free heap memory by destroying unreachable objects(unreachable objects are objects that are no longer being referenced).

Implementation in Java: Garbage collection in Java works in the way that tracking only live objects and then treating everything else as garbage. Since all objects are allocated on the heap, Java maintains several object trees, where each tree may have one or more root objects. So as long as the program can reach a root, the whole tree starting from that root are reachable.

Implementation in Java

Garbage collection in Java works in the way that tracking only live objects and then treating everything else as garbage. Since all objects are allocated on the heap, Java maintains several object trees, where each tree may have one or more root objects. So as long as the program can reach a root, the whole tree starting from that root are reachable.

GC Root (garbage collection root) is a kind of special objects in Java that’s always reachable, and therefore each object that has a GC root is reachable by the program.

There are 4 kinds of GC roots in Java:

  • Local variables (that are kept alive by the stack of a thread) in the main method;
  • Active Java threads;
  • Static variables;
  • JNI References (Java objects that the native code has created as part of a JNI call, and objects thus created are treated specially since the JVM doesn’t know if it’s being referenced by the native code or not.)

To determine which object is no longer in use, the JVM intermittently runs a “Mark-and-Sweep Algorithm”, which contains two steps:

  1. Mark → Traverse all object references, starting with the GC roots, and marks every object found as alive;
  2. Sweep → Clear all heap memory that is marked as unreachable (not occupied by a live object) and set it to be free.

Garbage collection is intended to remove the cause for classic memory leaks: unreachable-but-not-deleted objects in memory. However, this works only for memory leaks in the original sense. It’s possible to have unused objects that are still reachable in the program if the developer forget to dereference them. Such objects can’t be garbage-collected. Such a logical memory leak can’t be detected by any software. Even the best analysis software can only highlight suspicious objects.

Memory Allocation: In Java, stack memory is responsible for holding references to heap objects and for storing primitive data types, and all objects are dynamically allocated on heap (when we declare a variable of a class type, only a reference is created and memory is not allocated. To allocate memory to an object, we must use new. So the object memory is always allocated on heap.) Usually OS allocates heap memory in advance to be managed by the JVM while the program is running. So object creation is faster since global synchronization with the OS is not needed for every single object. An allocation simply claims some parts of a memory array and moves the offset pointer forward. The next allocation starts from this offset and claims the next parts of the array.