JVM Learning Notes

Source: Internet
Author: User

One, multithreaded JVM instances

A JVM instance can contain a number of threads. An entity machine contains multiple instances of the JVM and binds different ports. Thread-Shared areas: Method area, constant buffer pool, heap. Per thread Private: program counter, Stack, local method stack.   JVM operating mechanism JVM execution Program: 1) load the. class file. 2) Manage and allocate memory 3) for GC. Two mechanisms of the JVM: 1) load the appropriate class or interface, called the class state subsystem. 2) Execute a class or interface that is already loaded, called the run engine.    three, runtime data region 1, JVM instance each thread Private program counter: The next execution of the bytecode, including branches, loops, jumps, exception handling, thread recovery and other basic functions. Virtual machine stack: Used to store local variable tables (various data types: int,float), operand stacks, dynamic links, method exits and other information. Local method Stack: The difference between the above is that the virtual machine stack executes the Java Method (bytecode) service, and the local method stack executes the native method service.  2, JVM All Threads common Java heap: store only a variety of object instances, GC's main area, divided into Eden,supervivor*2,old,permanent area. Method Area: Store the virtual machine loaded class information, constants, static variables, compiled code and other data. Chang: When a class is loaded into a method area, it is used to hold constants. 3, Direct Memory JDK1.4 added NiO class, based on the channel and buffer IO mode, using the native function directly using the out-of-heap memory.   Four, object and Oom problem 1, object creation      new instruction, will go to the constant pool to see if the class is loaded, parsed, initialized. The memory storage object is then allocated in the Java heap, and the object is init into the heap. All information about the object is put into the object header information.      thread local Allocation buffer (Tlab) to allocate buffers, each thread will have a buffer that allocates objects to use. 2. Object structure       Object Header (header): One is to store the object's own running data (hash code, GC generational age, lock status identification, thread hold lock, bias thread ID, bias timestamp), and the second is to store the class metadata pointer, Use this pointer to determine which class is the object.       Instance data (Instance): holds real data information.       Align fills (Padding): Placeholders only, can be ignored. 3. Access to Objects     &NBSP Java programs (threads) are specific objects that manipulate objects by reference data on the stack (thread-private).       Two ways to access Objects:     1) use handles. Java adds a handle pool to the heap, where the handle address of the object is stored in the reference. The advantage: The object is moved, only changes the handle pool pointer, the reference itself does not change.      2) direct pointer. A pointer to an object is stored directly in the instance data area. Advantage: Save the time overhead of pointer positioning.  4, common oom issues      outofmemoryerror   memory overflow issues.       The cause of this problem is:     1) heap overflow. Java Heap space. A large number of objects or large objects are caused by a full heap.           Troubleshooting: You can analyze a memory snapshot of a dump by using the memory image analysis tool.           Cause: There are two reasons why a real heap is too small (memory overflow) and does not meet production needs. The second is memory leak (leak). If it is the first, you can try to resize the heap first (-XMX and-XMS), if it is small, you should upgrade the hardware, or cancel the machine multi-instance. If it is memory Leak, that is the problem of develop.          2) The local method stack and the virtual machine stack overflow.           There are two kinds of errors, one is stackoverflowerror and the other is OutOfMemoryError. If you apply a stack depth that is greater than the JVM's specified stack depth, return to the first type. The second is returned if the JVM cannot acquire memory while it is in the expansion stack. General single-threaded application stack, are the first. The second problem is the use of stacks when creating multithreading.            Cause: One is the-XSS parameter adjustment stack memory capacity is too small, the second is to define a large number of local variables, and three is to create too many threads, allocate the stack when memory overflow. Can be viewed using jstack.      3) method area and constant pool overflow.           versions prior to JDK1.6 (with 1.6), constant pools are allocated in the permanent generation and can be limited by parameter-xx:permsize and-xx:maxpermsize.           The cause of the problem: in code, a large number of dynamic classes (class names) or constants are placed in this space. The second is that this area is configured too small.      4) native direct memory overflow.           can be configured via parameter-xx:maxdirectmemorysize. Classes Directbytebuffer Classes and unsafe classes that use direct memory allocate too much memory.           The cause of the problem: As with the above ideas.        V, garbage collector GC and memory allocation  1, object death      1) Reference counting algorithm: Each object has a reference counter, when there is a place to quote him to add 1, the reference fails to minus 1, For 0 represents the object death. But it is impossible to judge each other by circulating references.      2) accessibility analysis algorithm: GC roots is the starting point, GC roots to Object unreachable (no reference chain-graph theory), the object is judged suspended (two tokens represent death), when the virtual machine executed a finalize () Or the object does not overwrite this method, the object is currently alive. This method is called by the JVM and pushes the object to the fire pit. The JVM puts the object into the F-queue queue when it considers it necessary to push the object to the fire pit. The second time the garbage collection is carried out.            Supplemental notes: References.      1) Strong quote: New. This referenced object is not recycled.      2) Soft reference: useful for non-essential objects. The memory is being reclaimed when it overflows. I am not a developer, and I am not sure what this object is.      3) Weak references: Non-essential objects, surviving to the next GC. WeakReference class implements weak reference      4) virtual reference: An object instance cannot be obtained by a virtual reference. The purpose of setting this up is onlyWhen it is recycled, get a system notification. Phantomreference class implementation.  2. Memory allocation and recovery Strategy (focus)Eden Supervivor1 Supervivor2 old permanentYoung 8:1 oldPolicy: 1) MINORGC: The new object is placed in the Eden area. When Eden is full, start YOUNTGC or call MINORGC. clears the non-surviving objects and moves the surviving objects to the survivor area. Then tidy up the two districts of survivor. 2) In general, scavenge GC is triggered when a new object is generated and the Eden application space fails. 3) Full GC: Organizes the entire heap, including young, tenured, and perm. The full GC is slower than the scavenge GC, so the full GC should be minimized as much as possible. The full gc:tenured may be filled with the following reasons, the Perm field is full, System.GC () is displayed, and the domain allocation policy for the heap after the last GC changes dynamically4) Large objects directly into the old generation, to avoid duplication of time in Supervivor. Configuration parameter-xx:pretenuresizethreshold. 5) long-term survival of the object into the old generation. The configuration parameter-xx:maxtenuringthreshold=15, which represents 15 times YOUNGGC after entering the old generation, has been replicated back and forth in two supervivor areas. Advanced methods can be determined by dynamic age: objects above the average age, into the old generation. 3, garbage collection algorithm 1) mark-Clear 2) Copy 3) mark-organize 4) Generational collection hotspot application Algorithm 1) enumeration root node 2) Security point 3) security Zone 4, garbage collector serial,parnew, Parallel scavenge,serial old,parallel old,cms,g1 vi. JVM View performance commands

JPs

    • View all JVM processes, including process IDs, process-initiated paths, and so on.
    • I also use PS, namely: PS-EF | grep java

Jstack

    • Observe the operation of all current threads in the JVM and the current state of the thread.
    • The system crashed? If a Java program crashes generating a core file, the Jstack tool can be used to obtain information about the core file's Java stack and the native stack, making it easy to know how the Java program crashes and where the problem occurs in the program.
    • The system Hung live? The Jstack tool can also be attached to a running Java program to see the Java stack and native stack information for the Java program that was running, and jstack is useful if the Java program that is running now renders hung state.

Jstat

    • Jstat uses the JVM built-in instructions to monitor the resources and performance of Java applications in real time, including the classloader,compiler,gc of the process;
    • In particular, a powerful tool for monitoring memory can be used to monitor the size of various heap and non-heap sizes in VM memory and their memory usage, as well as the number of load classes.

Jmap

    • Monitor the consumption of the JVM's physical memory in the process running, the process in memory, the circumstances of all objects, such as which objects were produced, the number of objects;
    • The system crashed? Jmap can get the exact match of memory from the core file or process, including heap size, Perm size, etc.

Jinfo

    • Observe the process run environment parameters, including Java System Properties and JVM command-line arguments
    • The system crashed? Jinfo can know the configuration information of the crashed Java application from inside the core file.

Note

    • If you can skillfully use these commands, especially under Linux, you can completely replace the jprofile and other monitoring tools.
    • The advantage of the command is that it is fast and assists other commands, such as grep gawk sed, to assemble a variety of tools to suit your needs.
Vii. frequently asked questions can lead to the following common problems: 1, YOUNGGC frequent one is the service pressure, the number of threads, the definition of new objects, and quickly occupied Eden. The second is that Eden is too small. 2, YOUNGGC time long one is the data structure of the object is too large, the second is to apply for large objects, third, the definition of a useful object too much, put in the Eden area, in two Supervivor area to replicate memory consumption caused.     3, FULLGC frequent 4, FULLGC time is long. The heap is often assigned too large to cause. If your machine is good (64G memory above?) ), it is recommended that you can deploy more than 32-bit JVM instances or 64-bit JVMS to handle large memory (the latter is not good), too large a heap, take a long time to FULLGC, and the service experience is not good. 5. Memory overflow or memory leak

JVM Learning Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.