java architecture Road - (11) JVM and heap objects

  The last blog, we said jvm runtime memory model, heap, stack, program counter, and native method stacks element space. We say that the main heap and stack, stack flow generally also say it again, and we know that the heap is used to store objects, respectively, the young generation and the old era. But the concrete heap is how to store the object of it? When you can place objects in it's old. Now I look.

  

 

 

 If the default settings are, generally is the case. Suppose we set the memory heap size to 600M, so old it's probably the 400M, 200M is our young generations, then the young generation of eden region accounts is 200M of 8/10 160M, the new objects are generally in this, I I mean general ah. 600M will use this later described in detail, from and to the area each accounted for 20M, that is, the area occupied Survivor 40M, each finished minor GC, objects placed on this area.

  I just said, sometimes the object is not the young generation, so I specifically analyze what happens is placed in the young generation, and when they are placed in the old era.

1, after Minor GC, surviving Survivor target area fit.

public class Main {
    public static void main(String[] args) {
        byte[] bt1;
        bt1 = new byte[60000 * 1024];
    }
}

Join heap memory log, we get print results:

   We get bt1 new future, we heap memory is almost filled, and is now 99%, then we'll look at.

public class Main {
    public static void main(String[] args) {
        byte[] bt1,bt2;
        bt1 = new byte[60000 * 1024];
        bt2 = new byte[10000 * 1024];
    }
}

Then we can learn from the code, we built bt1 has built bt2, then our eden region should be enough, then our memory will be how to deal with it. We look at the results

  We can see already made a GC, but still does not fit, then we will direct large objects placed directly on the heap memory.

2, long-term survival of the object to the old era. That is, after repeated after minorGC, object or alive, we move the object's old home, usually 15 times, which is within the target age-generational head reach 15 years of age, we shift the object's old home.

3, to determine the age of dynamic objects.

  This is very important theoretical knowledge, it probably is, when we finished minorGC, objects are placed to the region, which is our Survivor's to the region, may be the object does not fit, then the classification will be calculated age, approximately It is to count the sum of all ages generational 1, plus 2 generational age, plus the generational age of 3, sequentially adding, to the maximum generational been aged, but adding the process, you'll find added to the generational age of m objects, the total size of the region has been filled up to this time will be m to n generational age objects are relocated to the old era, contains m. When an object is greater than 50% Survivor area, the back of the object that contains this age are placed in the old era.

4, large objects directly on the old era. Look at the code segment.

public class Main {
    public static void main(String[] args) {
        byte[] bt1;
        bt1 = new byte[90000 * 1024];
    }
}

  Above, I know we probably create a 600M object when placed in eden, accounting for 99%, then we create an object larger than the 600M, eden must fit inside. Well placed directly in the old era. Here is the parameters that can be set. I then look to set a parameter, set the parameters for

-XX:PretenureSizeThreshold=10000000 -XX:+UseSerialGC -XX:+PrintGCDetails   

public class Main {
    public static void main(String[] args) {
        byte[] bt1;
        bt1 = new byte[20000 * 1024];
    }
}

   We set the parameters, the statement 10M objects on a large object, we have created a rough target 20M, it is placed directly on the old era. Is the object of minorGC the experience so many times, jvm virtual opportunity to think you might have been alive, taking advantage of this time does not fit, you're over it as early as possible to our old's mix it.

5 years old space allocation guarantee mechanism.

  In fact, every time before we minorGC, there will be a series of operations that may be full GC, then we look at the process it.

 

 I explain above the colorful map. When we get the full eden region, the need for minorGC, then the remaining space will give priority to look at the size of old age, years old if the remaining space is running out, we might be full GC, which is the remaining space of our old age less than our eden region will be the sum of minorGC object.

If it is really small, then we go down, we will determine when configuring the -XX: -HandlePromotionFailure (jdk8 than the default setting) this parameter, if not configured directly fullGC, if configured to go to determine the remaining years old after each space is less than minorGC every time we want to put the average object size of the old era, if less than minorGC years old, then be fullGC. Otherwise, do not require full GC.

eden and Survivor (from and to) the default ratio is 8: 1: 1, but we might jvm parameter optimization, which is -XX: + UseAdaptiveSizePolicy the default parameters, I changed it to -XX: -UseAdaptiveSizePolicy not optimize, maintain the 8: 1 ratio of: 1.

  Let us look at what kind of objects can be recycled.

1, reference counting method (basic need, circular reference object is never destroyed, it may be out of memory)

  Add a reference to an object counter, whenever a reference to its place, the counter is incremented; when referring to failure, the counter is decremented by 1; any time the counter for the object 0 is no longer being used. 

  GC Roots root generally thread stack of local variables, static variables, local variables, and so the method stack.

2, reachability analysis algorithm.

  The basic idea is that through a series of algorithms called "GC Roots" objects as a starting point from which to start the search nodes down to locate the objects are marked as non-garbage objects, the remaining unmarked objects are garbage objects

3, common reference type.

  java reference type generally divided into four types: strong references, soft, weak, reference phantom reference 

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class Main {
    public static void main(String[] args) {
        User user = new User();//强引用
        WeakReference<User> user2 = new WeakReference<User>(new User());//弱引用
        SoftReference<User> user3 = new SoftReference<User>(new User());//软引用
    }
}

  The general type of object reference the object wrapped with SoftReference soft, not normally be recovered, but after the release of GC finish found no space for new objects, the objects will be recovered out of these soft references. Soft references are used to implement memory-sensitive caches. 

4, finalize the final determination target survival.

  finalize method is the last run before the object is about to be withdrawn, you can write logic, but absolutely do not recommend going to write this, it is likely never be recovered objects, causing memory overflow, that is also in the finalize method could "save" our object.

  Even unreachable in reachability analysis algorithm objects, and it is not "Feisibuke", and this time they are temporarily in a "probation" stage, to really declare an object death, at least to go through the process again mark. 

  How to determine the class of a class is useless

1. All instances of the class have been recovered, which is the Java heap any instance of the class does not exist.

2. Load the class ClassLoader has been recovered.

3. java.lang.Class corresponding to the class object is not referenced in any place, not by the method of accessing the class reflected anywhere. 

  Finally, we look at escape analysis.

    JVM runs in three modes, namely, explanatory models, compiled and mixed modes, here briefly about this issue, or behind Mong Circle.

Explanation model is executed line JVM bytecode to machine code compile a behavior, this advantage is very save memory space without all the byte code memory are stuffed inside, low operating efficiency, but started fast.

Compilation mode and interpreted mode contrary, is first of all a compiled JVM byte code into machine code, and machine code to perform all at once. This will improve our operational efficiency, but space-consuming resources.

Mixed mode is the sum of the above, still executing code pattern using an interpreter, but for some "hot" code takes compilation mode execution, the JVM typically mixed mode code execution.

Let's look at a piece of code.

public  class the Main {
     public the User getUserBeanTest () { 
        the User User = new new the User (); // placed in a heap 
        return User; 
    } 


    public  void userBeanTest () { 
        the User User = new new the User (); // priority placed with hair and methods on the stack. 
    } 
}

That is clear, there is a very small objects may be placed on the stack. Mid-Autumn Festival holiday, up about mybatis tomorrow is to achieve the underlying principle. For a few days to continue to optimize our jvm

Guess you like

Origin www.cnblogs.com/cxiaocai/p/11520731.html