Memory overflow and memory leaks
Memory overflow: An error caused by the inability to request enough memory during a program's run. Memory overflows typically occur after the old or perm segment garbage collection, and there is still no memory space to accommodate new Java objects.
Memory leak: Refers to the program in the dynamic allocation of memory to some temporary objects, but the object is not reclaimed by the GC, it always consumes memory. That is, the allocated object can be reached but is useless
Package Com.weixuan.outofmemory;import Java.util.arraylist;import java.util.list;/** *-verbose:gc-xms20m-xmx20m- XMN10M-XX:+PRINTGCDETAILS-XX:SURVIVORRATIO=8 *-xx:+heapdumponoutofmemoryerror * * @author Nicholas * java heap overflow * java heap To store object instances, as long as the object is constantly created, and the GC roots to the object has reachable paths to avoid the garbage collection mechanism to clear these objects * then a memory overflow exception occurs when the number of objects reaches the maximum heap capacity limit *//* *-xms20m: The minimum heap value is 20m *- xmx20m: Heap maximum value is 20m, the maximum and minimum values are the same, can avoid heap auto-expansion *-xmn10m: Cenozoic size is 10m *-xx:+printgcdetails: Print auxiliary information *-xx:survivorratio=8:java heap The ratio of Eden to Survivor is set to 8, the ratio of two survivor to one Eden area is 2:8, and a survivor area is the 1/10 of the entire young generation *-xx:+heapdumponoutofmemoryerror : When a memory overflow exception occurs, dump the current memory dump snapshot information for post-analysis *//** * How to handle heap memory overflow? * Open heap Memory dump file with EMA Eclipse Memory Analyzer * First to distinguish between memory leaks or memory leaks * If it is a memory leak: The tool looks through the reference chain of the leaked object to the GC roots and then finds out what path the compromised object is related to the GC roots and causes the garbage collector not to automatically reclaim their * if there is no leakage, that is, the object in memory is still alive, then you should check the virtual machine's heap parameters-xmx-xms can be adjusted appropriately * */public class Heapoom {static Class Oomobj ECT {}public static void main (string[] args) {list<oomobject> List = new ARRAYLIST<HEAPOOM.OOMOBJECT≫ (); while (true) {List.add (New Oomobject ());}}}
Iii. method area and run-time constant pool overflow
V. Summary
Memory leaks in several scenarios: 1. Long life cycle objects hold references to short life cycle objects
This is the most common scenario for memory leaks and is a recurring problem in code design.
For example, in a global static map cache local variables, and there is no empty operation, over time, the map will grow larger, causing memory leaks.
2. Modify the parameter value of the object in HashSet, and the parameter is the field that computes the hash value
When an object is stored in the HashSet collection, it is not possible to modify those fields in the object that participate in the calculation of the hash value, otherwise the modified hash value of the object is different from the hash value originally stored in the HashSet collection, in which case Even if the Contains method uses the object's current reference as a parameter to retrieve an object in the HashSet collection, the result of the object cannot be found, which also results in the inability to remove the current object from the HashSet collection, causing a memory leak.
3, the machine's connection number and the shutdown time setting
Opening a very resource-intensive connection for a long time can also lead to memory leaks.
Memory overflow Scenarios: 1, heap memory overflow (Outofmemoryerror:java heap space)
In the JVM specification, memory in the heap is used to generate object instances and arrays.
If broken down, heap memory can also be divided into younger generations and older generations, including an Eden area and two survivor districts for younger generations.
When a new object is generated, the memory application process is as follows:
A, the JVM first attempts to allocate the memory required for new objects in the Eden area;
b, if the memory size is sufficient, the application ends, otherwise the next step;
C, the JVM initiates YOUNGGC, attempts to release the inactive objects in the Eden area, and if Eden space is still insufficient to place the new objects after release, attempts to put some of the active objects in Eden into the Survivor area;
D, Survivor area is used as the middle Exchange area of Eden and old, when the old area is enough space, the object of Survivor area will be moved to old area, otherwise it will be kept in survivor area;
E, when the old area is not enough space, the JVM will be in the old area full GC;
F, full GC, if the survivor and old areas still cannot hold portions of objects copied from Eden, causing the JVM to be unable to create memory areas for new objects in the Eden area, an "out-of-storage error" occurs:
Outofmemoryerror:java Heap Space
2. Method area Memory Overflow (outofmemoryerror:permgem space)
In the JVM specification, the method area mainly holds the class information, the constant, the static variable and so on.
So if a program loads too many classes, or uses such dynamic proxy generation techniques as reflection, gclib, and so on, it can cause a memory overflow in that area, which is generally the error message when a memory overflow occurs in this area:
Outofmemoryerror:permgem Space
3. Thread stack Overflow (java.lang.StackOverflowError)
Thread stacks a piece of memory structure that is unique to threads, so a thread stack problem must be an error that occurs when a thread is running.
Generic thread stack Overflow is caused by too much recursion or too many levels of method calls.
The error message that the stack overflow occurred is:
Java.lang.StackOverflowError
How to avoid memory leaks
1. Release references to useless objects as early as possible
2, use string processing, avoid using string, should use a lot of stringbuffer, each string object has to occupy a piece of memory area
3, minimize the use of static variables, because static variables stored in the permanent generation (method area), the permanent generation of basic non-participation in garbage collection
4. Avoid creating objects in loops
5. Opening large files or taking too much data from a database can easily cause memory overflow, so be sure to calculate the maximum amount of data in these places, and set the minimum and maximum memory space values required.