javamemoryjvmgarbage-collectionjvm-hotspot

Does Garbage Collectors (GC) collect only objects or also overwrite the data stored by it?


HotSpot's Garbage Collectors (GC)

When HotSpot's Garbage Collectors (GC) runs does it only collect objects or overwrite stored data to prevent memory dumping?


Solution

  • This is a rather broad question because there are many different algorithms for GC.

    Let's take Hotspot and G1 as an example.

    A minor GC copies objects from Eden space to a survivor space, between survivor spaces and promotes objects to the old generation (depending on object age). In all of these, the memory used by those objects will subsequently be overwritten but not deallocated.

    A major GC will copy objects from one region (which is a logical area of memory) to another to compact objects, eliminating fragmentation. Again, the memory used by these objects will be overwritten at some point in the future.

    Some collectors like Zing from Azul (who I work for) uncommit unused memory when the heap usage shrinks and all allocated pages are no longer required. Not all GCs do this, though. This returns memory pages to the OS allowing them to be used for other applications.