One, the JVM memory area composition

Source: Internet
Author: User
Tags xms

One, the JVM memory area consists of four kinds of memory: 1, Stack (stack segment)-Automatically allocated by the compiler release, stored function parameter value, local variable value, etc., after the execution of the method, the system automatically frees the memory resource 2, heap area (heap Segment)-typically released by programmers, storing objects and arrays created by new, the JVM does not periodically view this object, if no reference to this object is recycled 3, static zone (data segment)-Holds global variables, static variables and string constants, does not release 4, Code Segment-binary code that stores methods in the program, and multiple objects sharing a code space area
When a variable is defined in a method (code block), Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees up the allocated memory space for that variable, and the memory allocated in the heap is managed by the Java Virtual machine's automatic garbage collector. The advantage of a heap is that it can dynamically allocate memory size, and the lifetime does not have to tell the compiler beforehand because it allocates memory dynamically at run time. The disadvantage is to dynamically allocate memory at run time, the access speed is slow, the advantage of the stack is faster than the heap, the disadvantage is that there is a stack of data size and lifetime must be determined without flexibility.
The Java heap consists of the perm and the heap, and the heap is made up of the old and new regions, and the new zone is divided into the Eden Zone, from Zone, to zone, Heap = {old + NEW = {Eden, from, to}, as shown in Figure 1.

The heap distinguishes between two chunks, one is NEW Generation and the other is old Generation. In new generation, there is a space called Eden, which is mainly used for storing new objects, and two survivor Spaces (From,to), which are used to store objects that survive each garbage collection. In old Generation, a memory object with a long life cycle in the application, and a permanent Generation, are used primarily to put the JVM's own reflective objects, such as class objects and method objects.
In the new generation block, garbage collection generally uses the copying algorithm, which is fast. At each GC, the surviving objects are first copied from Eden to a survivor space, and when the survivor space is full, the remaining live objects are copied directly to the old generation. Therefore, after each GC, the Eden memory block is emptied. In the old generation block, garbage collection generally uses the mark-compact algorithm, which is slower, but reduces memory requirements.
Garbage collection sub-level, Level 0 for all (full) garbage collection, will reclaim the old section of garbage, Level 1 or above is a partial garbage collection, will only reclaim new garbage, memory overflow usually occurs after the old or perm segment garbage collection, still no memory space to accommodate the new Java object situation.
The frequency with which the JVM calls the GC is still high, with garbage collection in two main cases: when the application thread is idle, and when the Java memory heap is not sufficient, the GC is called continuously, and if continuous recycling does not solve the problem of insufficient memory, it will report out of error. Because this exception is determined by the operating environment of the system, it cannot be expected when it appears.
According to the GC mechanism, the operation of the program can cause changes in the operating environment of the system and increase the chance of the GC triggering. To avoid these problems, the design and writing of the program should avoid the garbage object's memory footprint and the GC overhead. The display call System.GC () can only suggest that the JVM needs to recycle the garbage object in memory, but it does not have to be recycled at once, one that does not solve the memory depletion situation, and also increases GC consumption.
When a URL is accessed, the memory application process is as follows: A. The JVM tries to initialize a chunk of memory in Eden for the associated Java object B. When the Eden space is sufficient, the memory request ends. Otherwise, the JVM attempts to release all inactive objects in Eden (this is a 1 or more advanced garbage collection), and if the Eden space is still insufficient to fit into the new object after release, attempt to put some of the active objects in Eden into Survivor D. The survivor area is used as an intermediate swap area for Eden and old, and when the old area is sufficiently large, the objects in the survivor area are moved to the old area, otherwise they will be kept in the survivor area E. When the old area is not enough space, The JVM will be fully garbage collected in the old area (level 0) F. After a complete garbage collection, if the survivor and old areas still cannot hold portions of objects copied from Eden, causing the JVM to fail to create a memory area for the new object in the Eden area, an "out of storage error" occurs
Second, memory overflow type1. Java.lang.OutOfMemoryError:PermGen space JVM manages two types of memory, heap and non-heap. Heaps are for developers, created at the start of the JVM, and non-heaps are left to the JVM to store information about the class. Unlike heaps, the GC does not free up space during the run time. If the Web app uses a large number of third-party jars, or if the application has too many class files, and the MaxPermSize setting is small, exceeding this will cause the memory to overflow too much, or the Tomcat hot deployment will not clean up the previously loaded environment, Only the context is changed to the newly deployed, and the non-stockpiled content becomes more and more.
2, Java.lang.OutOfMemoryError:Java heap space This memory overflow is one of the most common situations, mainly manifested in the old area overflow, the cause may be: 1) set the memory parameters too small (ms/mx, newsize/ Maxnewsize); 2) procedural issues. Heap space, which is the default space (that is,-XMS), is 1/64 of physical memory, and the maximum space (-XMX) is 1/4 of physical memory. If the memory is less than 40%,JVM the value of the heap to the XMX setting is increased, the memory remaining more than 70%,JVM will reduce the heap to XMS setting value. So the server's xmx and XMS settings should generally be set identically to avoid resizing the virtual machine heap after each GC. Assuming that the physical memory is infinite, then the maximum size of the JVM memory is related to the operating system, the general 32-bit machine is between 1.5g and 3g, and 64-bit will not be limited.
Note: If XMS exceeds the XMX value, or if the sum of the heap maximum and the non-heap maximum exceeds the physical memory or the maximum operating system limit, the server will not start up.
3. The Java.lang.outofmemoryerror:c heap space system has no restrictions on the C heap, so when the C heap problem occurs, the memory of the Java process continues to grow until all available system memory is consumed
4. Other JVMs have 2 GC threads. The first thread is responsible for recovering the young area of the heap. The second thread traverses the heap when the heap is insufficient and upgrades the young area to the older area.  The size of the older area is equal to-xmx minus-xmn, and the value of the-XMS cannot be set too large because the second thread is forced to run to degrade the performance of the JVM. Why do some programs frequently occur in GC?  For the following reasons: A, System.GC () or RUNTIME.GC () is called within the program.  b, some middleware software calls their own GC method, at this time need to set parameters to prohibit these GC.  C, Java's heap is too small, the default heap values are generally small. D, frequent instantiation of??? Like, the release object.  At this point, try to save and reuse objects, such as using StringBuffer () and string (). If you find that after each GC, the remaining space in the heap will be 50% of the total space, which means your heap is in a healthy state. Many server-side Java programs have a better 65% space left after each GC
Iii. How the JVM sets up virtual memoryTip: This exception message is thrown in the JVM if 98% of the time is used for GC and the available heap size is less than 2%.  Tip: The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4. Tip: The initial allocation of memory by the JVM is specified by-XMS, which defaults to the maximum allocated memory for 1/64;JVM of physical memory specified by-XMX, which defaults to 1/4 of the physical memory. When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS.  So the server generally sets-xms,-xmx equal to avoid resizing the heap after each GC. Tip: Assuming that the physical memory is infinitely large, the maximum value of the JVM memory is very much related to the operating system. In short, the 32-bit processor, although the controllable memory space has 4GB, but the specific operating system will give a limit, This limit is generally 2GB-3GB (generally under Windows system is the 1.5g-2g,linux system for 2G-3G), and processors over 64bit will not have a limit hint: note: If XMS exceeds the XMX value,  Either the sum of the heap maximum and the non-heap maximum value exceeds the physical memory or the maximum operating system limit will cause the server to start up. Tip: Set newsize, Maxnewsize equal, "new" size is best not more than half of "old", because the old area if not enough to trigger the "main" GC, greatly reducing the performance tip: increase the size of the heap, although the frequency of the GC will be reduced, But it also increases the time of each GC.  And when the GC runs, all the user threads are paused, that is, during the GC, the Java application does not do any work. Tip: The Heap Size does not determine the amount of memory used by the process.  The memory usage of the process is greater than the value defined by-XMX because Java allocates memory for other tasks, such as the stack for each thread. Tip: Each thread has his own stack,stack size limits the number of threads. If the stack is too large, it will cause memory leaks. The-XSS parameter determines the stack size, such as-xss1024k. If the stack is too small, it can also cause the stack to spill.
The JVM uses-xx:permsize to set the non-heap memory initial value, which defaults to 1/64 of the physical memory, and the maximum non-heap memory by Xx:maxpermsize, which by default is 1/4 of physical memory. Workaround: Manually set the heap size to modify Tomcat_home/bin/catalina.bat on the "echo" Using catalina_base: $CATALINA _base "" Add the following line above: java_opts= "- server-xms800m-xmx800m-xx:maxnewsize=256m "
Four, the characteristics of the non-robust code and solutions1. Release the reference of the useless object as soon as possible. A good idea is to use a temporary variable when the reference variable is automatically set to null after exiting the active domain, implying that the garbage collector collects the object to prevent a memory leak. For instances where pointers are still pointing, the JVM does not reclaim the resource because garbage collection increases the efficiency of the GC recovery mechanism by using the null object as garbage; 2. We will inevitably use a lot of string processing in our program, avoid using string, and use StringBuffer extensively. Each string object has to occupy an area of memory independently, for example 3, to minimize the use of static variables, because the static variables are global, the GC will not be recycled, 4, avoid the centralized creation of objects, especially large objects, the JVM will suddenly need a lot of memory, it will inevitably trigger GC optimization system memory Environment  , the declared array space is displayed, and the number of requests is significant.  5, try to use the object pool technology to improve the system performance; objects with long life cycles are prone to memory leaks when they have short life-cycle objects, such as when large collection objects have large data volumes of business objects, consider chunking and then resolve a piece of policy that frees a piece. 6. Do not create objects in frequently invoked methods, especially when creating objects in loops. You can use Hashtable,vector to create a set of object containers, and then fetch those objects from the container, instead of discarding 7 each time after new, it usually happens when you open large files or take too much data with the database at once, causing out of Memory Error condition, At this point, you probably want to calculate the maximum amount of data, and set the minimum and maximum memory space values.

First, the JVM memory area consists of

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.