Java Garbage Collection
Long story short... All variable memory allocation is saved in a memory heap. Once the memory heap is full, Java would throw away the memory space which can't be referenced by any pointer or variable in the program.
Automatic Garbage Collection:
- Marking: garbage collector identifies which pieces of memory are in use and which are not.
- Normal Deletion: Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.
- Compacting: By moving referenced object together, this makes new memory allocation much easier and faster.
Generation Garbage Collection: motivation is - fewer and fewer objects remain allocated over time.
- The heap parts are: Young Generation, Old or Tenured Generation, and Permanent Generation
- Young generation: where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.
- Old Generation: The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.
- Permanent Generation: The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application.