Performance optimization|A picture shows you how JVM allocates memory

Priority distribution of objects

  • If the object can be allocated in the eden area, then it is directly allocated in the eden area
  • If the size of the eden area is not enough for new object storage, a minor gc will be triggered,
  • If it is found after minor gc that the object cannot be allocated in the s area, it will be allocated directly in the old generation
  • If the old generation cannot be stored, full gc will be triggered

Under what circumstances will objects be allocated directly in the old generation?

  • Large objects will directly enter the old age.
    What is called a large object? This is determined by the parameter we configured
    -XX:PretenureSizeThreshold=xxx.
    If the newly allocated object exceeds this value, the object will be allocated directly in the old age. The purpose is to reduce the gc time loss caused by the back and forth copy of large objects. It should be noted that this parameter only takes effect under the serial and parNew collectors.

  • Long-lived objects will directly enter the old age

    • How to judge the object is long-term survival?
      The virtual machine assigns a field to the head of each object to record the age of the object. The default age is 1. After a minor gc, if the object is still alive, the age is increased by 1;

    • If the age of the object reaches the value configured by the -XX:MaxTenuringThreshold parameter, which is 15 by default, it will directly enter the old age

  • Dynamic age judgment, to decide whether to enter the old age The
    dynamic age judgment starts after minor gc.
    After each minor gc, it will be sorted according to age, and the memory space will be accumulated from the youngest age. If the memory space is accumulated to an object with an age of 10, the memory space has exceeded 50% of the s area, and this time All surviving objects older than age 10 move into the old age;

  • After minor gc, the s area cannot be put down and will move directly to the old age

What is the space allocation guarantee mechanism?

Before each minor gc, jvm will calculate the remaining available space in the old generation. If the available space is less than the sum of all objects in the young generation, if the guarantee parameter ("-XX:-HandlePromotionFailure) is not configured, the full will be executed directly at this time gc, if the guarantee parameters are configured, full gc will not occur immediately. At this time, it is necessary to determine whether the available space in the old generation is greater than the average size of the old generation after each minor gc. If it is greater than that, the minor gc is still executed, otherwise Execute full gc, the flow chart is as follows:
Insert picture description here

How to determine whether an object has been recycled

  • Reference counting method
    Add a reference counter to an object. Whenever there is a reference to it, the counter is incremented by 1; when the reference is invalid, the counter is decremented by 1. Anytime an object whose counter is 0 is impossible to be used.
    This method is simple to implement and efficient, but the current mainstream virtual machine does not choose this algorithm to manage memory. The main reason is that it is difficult to solve the problem of circular references between objects.

  • Reachability analysis algorithm
    The basic idea of ​​this algorithm is to use a series of objects called "GC Roots" as a starting point. From these nodes, search downwards. All the objects found are marked as non-junk objects, and the remaining unmarked objects are all It is the root node of garbage object GC Roots: local variables of thread stack, static variables, variables of local method stack, etc.

Search on WeChat [AI Coder] Follow the handsome me and reply [Receive dry goods], there will be a lot of interview materials and architect must-read books waiting for you to choose, including java basics, java concurrency, microservices, middleware, etc. More information is waiting for you.

Guess you like

Origin blog.csdn.net/weixin_34311210/article/details/109438842