Part 2 - Java Garbage Collection Process

Memory Allocation

  1. Thread Local Allocation Buffers (TLAB):

  • Definition: TLABs are small memory buffers allocated for each thread in the Young Generation. They allow threads to allocate memory for new objects quickly without needing to synchronize with other threads.
  • Benefits: By using TLABs, memory allocation becomes faster and reduces contention among threads, which can lead to better performance, especially in multi-threaded applications. Each thread can allocate memory within its own TLAB until it fills it up, at which point it may need to request a new buffer.


2. Direct Allocation to Eden Space:

  • Eden Space: This is part of the Young Generation where all new object allocations occur. It is designed for high throughput and low latency.
  • Object Lifecycle: When an object is created, it is allocated directly in Eden Space. If it survives a garbage collection cycle, it is then moved to one of the Survivor Spaces. The frequent allocation and collection in this area help optimize memory usage for short-lived objects, which are common in many applications.


3. String Pool and Class Cache in Non-Heap Memory:

  • String Pool: The String Pool (also known as the String Intern Pool) is a special storage area in the heap where Java stores string literals. When a string is created using string literals, Java checks the pool first. If the string already exists, it returns the reference to that string, thereby saving memory and avoiding duplicate string objects.
  • Class Cache: This is used to store metadata about loaded classes. It allows for faster access and reduces overhead associated with class loading and unloading, which is particularly beneficial for applications that dynamically load classes.


GC Roots

  • Understanding GC Roots: GC roots are references that the garbage collector uses as starting points to trace and mark reachable objects in memory.


  • Local Variables from Thread Stacks: Each thread has a stack that holds local variables. These variables are directly accessible and are considered reachable objects.


  • Static Variables: Static variables belong to classes rather than instances and remain in memory for the duration of the class’s lifecycle. They are also considered reachable.


  • Active Thread References: References to active threads in the application help identify objects that these threads are using.


  • JNI References: Java Native Interface (JNI) references link Java objects to native resources, and these are also treated as roots since they can prevent objects from being collected.


Young Generation Collection

  • Eden Space and Survivor Spaces:

  • Eden Space: Where new objects are allocated. A minor garbage collection (GC) occurs when Eden fills up.
  • Survivor Spaces: After a minor GC, surviving objects in Eden are moved to one of the two Survivor Spaces (often referred to as "From" and "To"). Objects can be copied between these spaces in successive collections, allowing the garbage collector to reclaim space efficiently.
  • Object Aging Process:Objects that survive multiple minor GCs are considered "aged." After roughly 15 collections (this number can vary depending on JVM settings), these objects are promoted to the Old Generation, where they are less frequently collected, reducing the overhead of managing long-lived objects.


Old Generation Collection

  • Phases of Major GC:

  1. Mark Phase: The garbage collector starts from the GC roots and traverses the object graph to identify all live (reachable) objects. This process marks these objects to prevent them from being collected.


2. Sweep Phase: After marking, the collector goes through the heap and removes objects that are not marked as live. This frees up space for future allocations.


3. Compact Phase: After sweeping, the collector may compact the memory by moving live objects together, eliminating fragmentation. This helps optimize memory usage and allocation performance by creating larger contiguous blocks of free memory.Non-Heap Memory Management

  • Metaspace:Metaspace replaces the older PermGen space and is used for class metadata. Unlike PermGen, which had a fixed size, Metaspace can grow dynamically based on the requirements of the application. This flexibility allows applications to load many classes without running out of memory.
  • Class Cache:This stores information about loaded classes, which helps reduce the performance overhead associated with class loading. By caching this information, the JVM can access class metadata quickly, improving performance for applications that make extensive use of reflection or dynamically loaded classes.


Non-Heap Memory Management

  • Metaspace:Metaspace replaces the older PermGen space and is used for class metadata. Unlike PermGen, which had a fixed size, Metaspace can grow dynamically based on the requirements of the application. This flexibility allows applications to load many classes without running out of memory.


  • Class Cache:This stores information about loaded classes, which helps reduce the performance overhead associated with class loading. By caching this information, the JVM can access class metadata quickly, improving performance for applications that make extensive use of reflection or dynamically loaded classes.


Application Integration

  • Memory Interaction:This involves understanding how applications interact with memory during execution.


  • Thread Stack Relationships: Each thread has its own stack that holds method frames, local variables, and intermediate results. This isolation allows for concurrent execution while maintaining data integrity.


  • Object Allocation Flow: Understanding how objects are allocated (in Eden), how they move between generations (to Survivor Spaces and then to Old Generation), and how they are collected provides insights into optimizing performance and memory usage in Java applications.


claude.ai


Jerome Thomas

Technical Manager - Performance Engineering

4 个月

Pratik Ugale - Good work! I like the way you categorically arranged the whole process.. Let me throw some light on the number of GC cycles before promotion to Old GC. The number of garbage collection (GC) cycles required to promote objects from the young generation to the old generation in Java heap space is not fixed and depends on several factors: Factors affecting promotion: Tenuring threshold: This is a configurable parameter that determines the number of times an object must survive a minor GC cycle before being promoted. The default value is typically 15, meaning an object will be promoted after surviving 15 minor GC cycles. Survivor space size: If the survivor space fills up before the tenuring threshold is reached, objects will be promoted prematurely. Object size: Large objects may be directly allocated to the old generation to avoid frequent copying between generations. Garbage collector algorithm: Different GC algorithms have different promotion strategies. The exact number of GC cycles required for promotion can vary depending on the factors mentioned above. However, it is generally safe to assume that objects will be promoted after surviving a few minor GC cycles, typically around 15.

要查看或添加评论,请登录

Pratik Ugale的更多文章