Java heap space is a very painful problem, if the development of debugging encountered fortunately, if the project is running after a period of time to throw the exception, it is really tragic (then you have to find out in the code where the memory leaks), this is really a sad story.
1.java head space heap memory overflow
Analysis
This problem occurs because there are too many objects created by the Java Virtual machine, and the virtual machine allocates enough memory space to the heap for garbage collection, which is related to heap space.
Workaround:
1. Increase the size of the XMS (initial heap size) and XMX (maximum heap size) parameters in the Java Virtual machine. such as: Set java_opts=-xms256m-xmx1024m.
2. Check the program to see if a memory leak occurs (object is not released, collection is too large, network connection is not closed, algorithm has problem ...) ), find out where to leak, modify the program.
The first method applies to the scenario:
The JVM's maximum memory setting is too small (the general project is set to 1G is sufficient, and the exception that occurs is a bit abnormal).
scenario for the second method:
Object memory cannot be freed by the Java Virtual machine and is growing, taking up a lot of space, resulting in insufficient JVM memory.
2. Using the Mat tool to locate memory leaks learn about the JVM garbage collection mechanism
Java does not need to manually manage program memory like C, it has its own garbage collection mechanism: running asynchronous threads every once in a while to reclaim garbage to free memory does not require us to assist in reclaiming memory, but this does not mean that programmers can write code at will without worrying about memory recycling. Because Java garbage collection is conditional, here's a brief introduction to the garbage collection mechanism:
1.java virtual machines call a garbage collection at intervals, reclaim memory data that is not referenced, and release the memory.
2. In the case of low Java memory, garbage collection is called frequently, freeing up memory and throwing Java head Space exceptions when there is less than 2% of memory in the heap.
3.java garbage collection can display calls by means of "System.GC ()".
Locating memory leaks
Memory leaks: Object memory cannot be freed by the Java Virtual machine and is growing, taking up a lot of space, causing the JVM to have insufficient memory to throw Java head space.
Use of Mat 1. Memory Leak code example
[Java]View PlainCopy
- Package test;
- Import Java.util.HashMap;
- Public class oomtest{
- hashmap<integer,string> map = new hashmap<integer,string> ();
- public static void Main (string[] args) throws interruptedexception {
- new Oomtest (). Addmap ();
- }
- public void Addmap () throws interruptedexception{
- int i = 0;
- While (true) {
- Thread.Sleep (50);
- i++;
- Map.put (i, string.valueof (i));
- }
- }
- }
2. Export the dump file Snapshot runner and export the dump file before the program closes (after the Web project can throw an exception). Export File command: 1. Execute the JPS command in CMD (the Web project is to choose the published Tomcat,tomcat in JPS name "Bootstrap") 2. Execute Export command: jmap-dump:format=b,file= Export file path < Process number >
3. Use the MAT analysis file to install the Eclipse Mat plugin, select the dump file in the Eclipse file-> Open file, and load the dump. Click Dominator_tree, expand Memory (retained heap) max, and you can see that the pointer to the class is test. Oomtestmat Analysis Tips 1. Make the JVM memory large so that the mat shows the memory footprint at the memory leak is more obvious and excludes interference from other factors. 2. In actual use, you can obtain a snapshot of memory at multiple moments, before and after comparison, if an object has been growing before and after, indicating that there is a possibility of memory leaks.
3. Other common Oom and solution ideas:
1. OOM for perm=> example: Java.lang.OutOfMemoryError:Java Perm space
Analysis
The original intention of this problem is that the program uses a large number of jar or class, so that the Java Virtual machine load class space is not enough, and permanent Generation space.
Workaround
1. Increase the size of the xx:permsize and xx:maxpermsize parameters in the Java Virtual machine, where xx:permsize is the initial permanent save area size, Xx:maxpermsize is the maximum persistent area size.
2. If the JVM is running for a long time instead of just after it has been started, it is likely that the runtime has a class that is dynamically loaded, and it is recommended to uninstall the configuration with the classes in the CMS policy.
such as:-xx:+useconcmarksweepgc-xx:+cmsclassunloadingenabled
2. OOM for gc=> Example: Java.lang.OutOfMemoryError:GC overhead limit exceeded
Analysis
This oom is due to the fact that the JVM has too many objects in the GC to cause memory overflow, it is recommended to adjust the GC's policy to start GC at a certain scale instead of using the default policy, or to set the appropriate size for the new generation and the old generation, and to fine-tune the survival rate.
Workaround
Changing the GC policy, when the older generation is 80%, starts the GC and sets-xx:survivorratio (-xx:survivorratio=8) and-xx:newratio (-xx:newratio=4) more reasonable.
3. OOM for Native thread created=> example: java.lang.OutOfMemoryError:unable to create new native thread
Analysis
Refer to the following:
(maxprocessmemory-jvmmemory-reservedosmemory)/(Threadstacksize) = number of threads
Maxprocessmemory refers to the maximum memory of a process
Jvmmemory JVM Memory
Reservedosmemory reserved Operating system memory
Size of the Threadstacksize line stacks
If the JVM memory is too large or the utilization rate is less than 20%, it is recommended to lower the maximum heap and perm, and the line stacks small, that is,-XSS small, such as:-xss128k
Workaround
Under the premise that the JVM memory cannot be reduced, the-XSS setting is small, for example:-xss:128k
4. OOM for allocate huge array=> example: Exception in Thread "main": java.lang.OutOfMemoryError:Requested array size exceeds VM limit
Analysis
This information indicates that the application (or APIs called by the application) attempted to allocate an array larger than the heap size. For example, if the application is new to an array object with a size of 512M, but the maximum heap size is 256M, OutOfMemoryError is thrown because the size of the array exceeds the limit of the virtual machine.
Workaround
(1), first check the heap-xmx is not set too small
(2), if the heap-xmx is already large enough, then check the application is not a bug, for example: The application may be in the calculation of the size of the array, there is an algorithm error, resulting in a large array size, resulting in a large array is allocated.
5. OOM for small swap=> example: Exception in Thread "main": Java.lang.OutOfMemoryError:request <size> bytes for <re Ason>. Out of swap space
Analysis
This type of error is thrown due to a failure to allocate memory from the native heap, and the heap memory may be nearly exhausted. This type of error may not be related to the application, such as the following two reasons that can also cause errors to occur:
(1) The operating system is configured with a smaller swap area
(2) Another process in the system is consuming all the memory
Workaround
(1), check the OS swap is not set or set too small
(2), check if there are other processes consuming large amounts of memory, resulting in insufficient allocation of current JVM memory.
Note: Although sometimes the <reason> section shows the cause of oom, but most of the time,<reason> shows the name of the source module that prompted the assignment to fail, it is necessary to view the log file, such as the HS file when crash.
Memory Analyzer Tool locates the Java heap space leak