Java memory Composition
1) Heap
In the runtime data area, the memory of all class instances and arrays is allocated here. Created when the Java Virtual Machine is started. The object heap memory is recycled by the automatic memory management system called the garbage collector.
The heap consists of two parts:
Among them, Eden + fromspace + tospace is also called Young, and old space is called old generation.
There are also S1 and S0 (which will be seen in the JDK's built-in tool output), which respectively refer to the same vor space and store the surviving objects after each garbage collection.
Old Generation, mainly used to store long-lived objects in applications
Garbage collection is mainly used to recycle young generation blocks and old generation block memory. YG is used to store newly generated objects. After several collections, the uncollected objects are moved to og,
Garbage collection for YG is also called minorgc, while for OG garbage collection, it is called majorgc. The two memory collections do not interfere with each other.
2) Non-heap memory
JVM has a method area shared by all threads. The method area is non-heap memory. It stores the structure of each class, such as the runtime data pool, field and method data, and Code of methods and constructor methods. It is created when the Java Virtual Machine is started.
In addition to the method area, Java Virtual Machine implementation may need to be used for internal processing or optimization of memory, which is also non-heap memory. For example, the JIT compiler requires memory to store the locally converted code from the Java virtual machine code to achieve high performance.
Permanent generation (the permanent space in the figure) Stores JVM reflection objects, such as class objects and method objects.
3) Recycling algorithms and processes
JVM adopts a generational collection policy to scan and recycle young objects at a high frequency. This is called minor collection, the check frequency for old objects (old generation) is much lower, which is called major collection. In this way, you do not need to check all objects in the memory every GC.
When a URL is accessed, the memory application process is as follows:
A. JVM will try to initialize a memory area for the relevant Java object in Eden
B. When the Eden space is sufficient, the memory application is completed. Otherwise, go to the next step.
C. JVM tries to release all inactive objects in Eden (which belongs to 1 or more advanced garbage collection). If the Eden space is not enough to put new objects, try to put some active objects in Eden into the same vor Area
D. The primary vor area is used as the intermediate swap area of Eden and old. When the old area has sufficient space, objects in the primary vor area will be moved to the old area. Otherwise, objects in the primary vor area will be retained.
E. When there is not enough space in the old area, JVM will perform full garbage collection in the old area (Level 0)
F. after full garbage collection, if the primary VOR and old areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for the new object in the Eden area, "Out of memory error" appears"
Aging Process of objects
The memory of young generation is composed of an Eden (EDEN, interesting) and two pair vor spaces (semi-space in document 1.4. The memory of the newly created object is allocated from Eden. There will always be two idle vor spaces that are used as the destination space for copying collection. The minor collection process is to copy the Eden and the live objects in the consumer vor space to the idle consumer vor space. The so-called movie vor, that is, after the birth of most objects in the Garden of Eden, it cannot survive a GC. Object in young
After a certain number of minor collection times in generation, when the age is older, it will be moved to the old generation, called tenuring. (Is it true that the old object tenuring is used only when the primary vor space is insufficient? The description is not found in the current information)
If the remaining memory space is insufficient, GC is triggered. If the Eden space is insufficient, minor collection is required. If the old generation space is insufficient, major collection is required. If the permanent generation space is insufficient, full GC is triggered.
4. This section describes the analysis of the following errors on tomcat or other servers:
1. First, java. Lang. outofmemoryerror: Java heap Space
Explanation:
Heap size settings
JVM heap setting refers to the setting of memory space that JVM can allocate during Java program running. the JVM automatically sets the heap size value at startup. The initial space (-XMS) is 1/64 of the physical memory, and the maximum space (-xmx) is 1/4 of the physical memory. You can use options such as-xmn-XMS-xmx provided by JVM to set the configuration. The heap size is the sum of young generation and tenured generaion.
Tip: if 98% is used for GC and the available heap size is less than 2% in JVM, this exception is thrown.
Tip: the heap size should not exceed 80% of the available physical memory. Generally, you must set the-XMS and-xmx options to the same, and-xmn to the-xmx value of 1/4.
Solution:
Manually set heap size
Modify tomcat_home/bin/Catalina. BAT and add the following lines to "Echo" using catalina_base: $ catalina_base:
Java code
Set java_opts = % java_opts %-server-xms800m-xmx800m-XX: maxnewsize = 256 m
Set java_opts = % java_opts %-server-xms800m-xmx800m-XX: maxnewsize = 256 m
Or modify Catalina. Sh.
Add the following lines to "Echo" using catalina_base: $ catalina_base:
Java_opts = "$ java_opts-server-xms800m-xmx800m-XX: maxnewsize = 256m"
2. Followed by: Java. Lang. outofmemoryerror: permgen Space
Cause:
The full name of permgen space is permanent generation space, which refers to the permanent storage area of the memory. This memory is mainly used by JVM to store class and meta information, when a class is loaded, it will be placed in the permgen space. It is different from the heap region where the class instance is stored. GC (garbage collection) permgen space will not be cleaned up during the main program running period. Therefore, if your application has a class, the permgen space error may occur. This error is common in the web server pre-processing JSP.
Compile. If your web app uses a large number of third-party jar files and the size exceeds the default JVM size (4 MB), this error message is generated.
Solution:
1. manually set maxpermsize
Modify tomcat_home/bin/Catalina. BAT (Catalina. Sh in Linux ).
Add the following lines to "Echo" using catalina_base: $ catalina_base:
Set java_opts = % java_opts %-server-XX: permsize = 128 M-XX: maxpermsize = 512 m
Add the following lines to "Echo" using catalina_base: $ catalina_base:
Set java_opts = % java_opts %-server-XX: permsize = 128 M-XX: maxpermsize = 512 m
Catalina. sh:
Java code
Java_opts = "$ java_opts-server-XX: permsize = 128 M-XX: maxpermsize = 512m"
Java_opts = "$ java_opts-server-XX: permsize = 128 M-XX: maxpermsize = 512m"
JVM default settings
Heap (the sum of news generation and old generaion) settings
The initial allocated memory is specified by-XMS. By default, it is 1/64 of the physical memory but less than 1 GB.
The maximum allocated memory is specified by-xmx. By default, it is 1/4 of the physical memory but less than 1 GB.
By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx can be specified by-XX: minheapfreeratio =.
By default, when the free heap memory is greater than 70%, the JVM will reduce the minimum heap limit to-XMS, which can be specified by-XX: maxheapfreeratio =.
Generally, the server sets-XMS and-xmx to be equal to each other to avoid adjusting the heap size after each GC. Therefore, the above two parameters are useless.
-Xmn: Set the heap size of young generation.
-XX: minheapfreeratio and-XX: maxheapfreeratio set the percentage of idle memory to the total memory. These two parameters will affect the GC frequency and the time consumed by a single GC. -XX: newratio determines the ratio of young to old generation. The larger the young generation space, the lower the minor collection frequency, but the smaller the old generation space may increase the major collection frequency. -XX: newsize and-XX: maxnewsize directly specify the default and maximum sizes of young generation.
Non-heap memory settings
The default value is 64 MB.
-XX: permsize sets the minimum allocation space,-XX: maxpermsize sets the maximum allocation space. Generally, these two values are set to the same to reduce the time for applying for memory space.