2, Understanding the JVM

Source: Internet
Author: User

First, memory management:1, Memory structure: Stack and heap difference, stack is contiguous memory area, generally is 2M units, heap is discontinuous list. Limited to virtual memory, new allocates the PC register, Java stack, heap, method area, local method area, run constant pool Java stack: corresponding to a thread, stack frame in each stack associated with each method, run a method to create a stack frame, after executing the stack frame popup. Not thread sharing, no concern for data consistency and synchronization locks for these threading issues. Heap: Program apes are most concerned with the creation of new objects and arrays of objects, and all threads are shared to ensure consistency. Method Area: Store class structure information, such as class file parsing, constant pool, method data, method body, constructor, instance initialization and so on. Constant area: a constant table for each class file, which is part of the method area. That is, the Chang of the method area is one thing.  The local method stack, prepared for running the native method, is similar to the Java stack. 2, memory allocation policy: Static allocation: When the program compiles and joins, it is not allowed to have mutable data structures, such as mutable arrays, or to allow nesting or recursion. C language, source file size, and compilation generate another size stack memory allocation: The program is unknown to the data area compile time, but the runtime knows it, but the required data area size must be known when entering a program module. It is also an advanced post-stack allocation: You will not know the size of the space until you run to the appropriate code. 3,java memory allocation: Heaps and stacks are not generally cared for, and are corresponding to threads. Faster than the speed, second only to register. The data size and lifetime of the stack frame must be determined and inflexible. Heap, program apes are the most concerned, each Java application corresponds to a unique JVM instance, and each instance uniquely corresponds to a heap. Of course shared by all threads of this app. Released due to garbage collection GC. Generally speaking, the stack to execute the program, a stack corresponding to a thread, heap to hold the object. The heap requests the operating system to allocate memory, so it takes time to allocate and destroy, so it is inefficient. The advantages are flexible and variable. second, memory recovery1, the static memory recovery is automatic, the method ends, the corresponding stack frame is also revoked. 2, the dynamic memory reclaims whether the object is used and when it is recycled. Objects that are not referenced, that is, cannot be reached. These objects will be recycled A memory leak is a quoted, accessible. But useless, the program does not use them. is not recycled by GC, which accounts for memory waste. third, based on the sub-generational GC algorithmDivided into groups, young and old, several times after recovery also survived, put into the old group, the elderly group cell phone frequency is not so high. Different collection algorithms are used for different zones. 1, serial in client mode default, single-threaded complete, JVM other applications are paused. Suitable for limited memory, recycling 2 slower, parallel in server mode by default, multi-threading, other applications are also paused. High efficiency, the heap is too large, the pause time is 3, the concurrent concurrency number defaults to 4old zone pause time period, but the memory fragmentation, consumption CPU4,G1 collector G1 is the current garbage collection technology development of one of the latest achievements, and the previous several GC the biggest difference is: G1 can manage the entire heap area, Including the new generation and the old age. G1 is not physically distinguishable from the Cenozoic and the old ages. G1 will divide the whole heap into regions, and the new generation and the old have now changed only the concept of logic, and they do not need to be physically strictly differentiated. G1 will recycle all region and prioritize the recovery of the most efficient region。 In addition, G1 and CMS are also performed concurrently by GC, that is, you can perform cleanup concurrently (concurrently) with the user thread, but G1 can do a shorter pause time than the CMSGC Combination Practice: The hotspot JVM in server mode specifies different GC1 for the new generation and the old age, with concurrent 2,-XX:UseG1GC  全部用G1GC-XMS is the size of the set memory initialization-xmx is the maximum amount of memory that can be used Setup Method:1,eclipse can set VM parameters to the Java program running in the current development environment edit the currently used JRE, enter in the default VM parameter: 2,tomcat, open the Bin folder under Tomcat root for the application under Tomcat, Edit Catalina.bat, in setjava_opts=%java_opts% .... After this sentence add: setjava_opts=%java_opts%-xms1024m-xmx1024m Iv. Memory Leaksis quoted, can be reached. But useless, the program does not use them. is not recycled by GC, which accounts for memory waste. how to detect JVM memory leaksHow to detect JVM memory leaks in Java. Currently, we often use tools to check the JVM memory leaks of Java programs. There are several tools on the market that specialize in checking for JAVAJVM memory leaks, and they basically work in a similar all the information of memory management is counted, analyzed and visualized by monitoring the application and release of all objects when the Java program is running . v. Memory leaks to be aware of when writing code0, a singleton + object pool is a good habit, regardless of database connection pool, HTTP connection pool, or REDIS,MQ message queue. It can be said that the single case + object pool is a panacea for performance optimization, which reduces resource usage and improves efficiency. 1, avoid using vectors, objects into a vector, if we just release the reference itself, then the vector still refers to the object. Also note the list, MAP2, and beware of inner classes if the inner class is referenced. Causes the entire object to be referenced, in fact the entire object may not be used. 3, note dead loops or recursive calls in code because I'm in C #, vectors and inner classes don't have to be considered in my recipes. 3,js also be careful about memory leaks. Similarly careful circular reference, for background personnel write JS, this is not a problem

2, Understanding the JVM

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.