Memory allocation policy

GC operation in Cenozoic and old age

New generation GC operations: Minor GC

  • It happens very frequently, very quickly.

Old GC operations: Full GC/Major GC

  • Often accompanied by at least one Minor GC;
  • It is typically 10 times slower than the Minor GC.

Priority in Eden district

  • Running out of Eden space triggers a Minor GC;
  • Vm parameters:
    • -Xmx: Maximum value of the Java heap;
    • -Xms: The minimum value of the Java heap;
    • -Xmn: Cenozoic size;
    • -XX:SurvivorRatio=8:Eden zone/Survivor zone = 8:1

Big object goes straight to the old age

Large objects are Java objects that require a large amount of contiguous memory space. The most typical large objects are long strings and arrays. Large objects are bad news for virtual machine memory allocation, as their frequent presence tends to trigger garbage collection in advance to obtain enough contiguous space to “place” them when there is not enough memory left.

  • Large object definition: Java objects that require a large amount of contiguous memory space. Like long strings or arrays.
  • Set the size limit for the object to go directly to the old age:
    • -XX:PretenureSizeThreshold: is in bytes.
      • Only Serial and ParNew collectors are available.
    • Purpose: Because the new generation uses the replication algorithm to collect garbage, large objects can be directly entered into the old age to avoid massive memory replication in Eden and Survivor regions.

Long-lived objects will enter the old age

  • Fixed object age: The virtual machine defines an age counter for each object that passes a Minor GC on a Survivor and ages +1 to –XX:MaxTenuringThresholdAfter setting the value, will be promoted to the old age, –XX:MaxTenuringThresholdThe default value is 15.
  • Dynamic object age determination: if the sum of Survivor Spaces for objects of the same age is greater than half of Survivor Spaces, then objects older than or equal to this age are directly promoted to the old age.

Space allocation guarantee

When a Minor GC occurs, the virtual machine checks whether the average size of each previous promotion to the old age is greater than the remaining size of the old age, and if so, performs a Full GC directly instead. If less, check whether the HandlePromotionFailure setting allows guarantee failure; If allowed, only Minor GC will be performed; If not, do a Full GC instead.

This section supplements knowledge

GC: Garbage collector

Minor GC: Refers to a garbage collection that occurs in the new generation. All Minor GCS trigger a stop-the-world, stopping the application thread, but this process is very brief.

Major GC/Full GC: Old GC refers to GC that occurs in the old era.

Minor GC process

At this time, if the new object cannot be created in Eden area (Eden area cannot accommodate), a Young GC will be triggered. At this time, it will analyze the reachablability of objects in S0 area and Eden area together, find the active object, copy it to S1 area, and empty the objects in S0 area and Eden area. This clears unreachable objects and swaps S0 and S1.

Major GC process

GC that occurs in older generations, basically every Major GC occurs every Minor GC. And Major GC tends to be 10 times slower than Minor GC.

  1. For a large object, we try to create it in Eden first, and if we can’t, the Minor GC is triggered
  2. Then I continued to try to store it in Eden area, but it still could not be placed
  3. Try to go straight to the old age, the old age can not put down
  4. Trigger the Major GC to clean up old age space
  5. Put down the success
  6. Not put OOM