Java Virtual machine-Memory Overflow exception

Source: Internet
Author: User

Memory Leakswith theMemory OverflowMemory Leakscode design is usually caused by defects, refers to the program in the dynamic allocation of memory to some temporary objects, but the object is not recycled by the GC, itAlways use memory. That is, the allocated object can be reached but is useless; by understanding the memory-leaking farmto avoid unnecessary memory overruns and improve your own code levels;several scenarios for memory leaks:
1. Long life cycle objects hold references to short life cycle objects
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 current reference to the object is used as a parameter in the Contains method to retrieve an object in the HashSet collection, the result of the object cannot be found, which also results in the inability to delete the current object from the HashSet collection, causing a memory leak;
3, the number of connections and the setting of the shutdown time, the long time to open a very resource-intensive connection, will also cause a memory leak. Memory Overflowrefers to the process of running a programUnable to request enough memorycaused by a mistake. Memory overflow usually occurs after the old or perm segment garbage collection, there is still no memory space to accommodate the new Java objects, and by understanding the common situation of memory overflow, you can quickly locate the location of the problem in the event of memory overflow and shorten the time to resolve the failure. several cases of memory overflow:
1. Heap Memory Overflow(Outofmemoryerror:java Heap Space)
The Java heap is used to store object instances, as long as objects are constantly created and the GC roots to the object to avoid the garbage collection mechanism from clearing these objects, then a memory overflow exception occurs when the number of objects reaches the maximum heap capacity limit.
In the JVM specification, the memory in the heap is used to hold object instances and arrays, and if broken down, heap memory can be divided into young generation and old age, and the young generation contains 1 Eden areas and 2 survivor districts;
When a new object is generated, the memory application process is as follows:
1.1:JVM first try to differentiate the memory required for the new object in Eden, and if the memory size is sufficient, then the application ends;
1.2: If the Eden area does not have enough memory for the new object, the JVM will start the minor GC, attempting to release the inactive objects in the Eden area, and if the Eden area is still insufficient to store the new objects after release, it will attempt to put the active objects in Eden into the Survivor area;
The 1.3:survivor area is used as an intermediate swap area for the Eden and old areas, and when the old area is sufficiently large, the objects in the Survivor area will be moved to the old area, otherwise they will be retained in the Survivor area;
1.4: If there is insufficient space in old area, it will trigger a mijor GC (full GC)
"Outormermoryerror" error occurs when the survivor and old areas are still unable to store some of the objects copied from Eden, resulting in the JVM not being able to create memory areas for new objects in the Eden area after 1.5:full GC;2, virtual machine stack and local method stack Overflow
The virtual machine stack and the local method stack are not distinguished in the hotspot virtual machine, soHotSpot, Although the-xoss parameter (set local method stack) exists, this parameter is actually invalid,stack capacity is only set by the-XSS parameter.
The Java Virtual Machine specification defines 2 exceptions for both the virtual machine stack and the local method stack:
2.1 If the stack depth requested by the thread is greater than the stack depth allowed by the virtual machine, the Stackoverflowerror exception is thrown;
2.2 If the virtual machine is not able to request enough memory space when it expands, it will throw a OutOfMemoryError exception;
Single thread Test, whether the stack depth is greater than the stack depth allowed by the virtual machine, or not enough memory space can be requested,Are thrown stackoverflowerror exceptions;
1. Use the-XSS parameter to reduce the stack memory capacity, throw stackoverflowerror exception; 2. Define a large number of local variables, increase the length of this method frame local variable table, throw the stackoverflowerror exception;
If the test is not limited to a single thread, it is possible to create a memory overflow exception by constantly creating threads;
The virtual machine provides parameters to control the maximum value of the two-part memory of the Java heap and the method area.
The remaining memory-xmx (maximum heap capacity)-maxpermsize (maximum method area capacity), the program counter consumes less memory. If the memory consumed by the virtual machine process itself is not counted, then the remaining memory is partitioned by the virtual machine stack and the local method stack.
the larger the stack capacity each thread allocates, the less natural the number of threads can be established, and the easier it is to run out of memory when the thread is established;
if it ismemory overflow due to multi-threaded setup, in cases where the number of threads cannot be reduced or a 64-bit virtual machine is replaced, onlyreduce the maximum heap and reduce stack capacity in exchange for more threads;
3. Method area and run-time constant pool overflow(Outofmemoryerror:permgen Space)
Running a constant pool is part of the method area.
In the JVM specification, the method area is mainly the information, constants, static variables of the storage class;
In JDK1.6 and earlier versions, run a constant pool allocation in a permanent generation, you can limit the size of the method area using-xx:permsize and-xx:maxpermsize, thereby indirectly limiting the capacity of the const pool.
As you can see in the test run results, the run-time pool overflow, followed by OutOfMemoryError, is PermGen space, indicating that the run-time pool is part of the method area (the permanent generation in the Hotspot virtual machine);
JVM Parameters:-xx:permsize=2m-xx:maxpermsize=2m
The size of the method area is set very low, and there is an out-of-memory condition when you start loading the class library
A method area is used to store information about a class, such as its name, access modifiers, Chang, field descriptions, method descriptions, and so on. For this area of testing, the basic idea is that the runtime produces a large number of classes to fill the method area until overflow;
If a program loads too many classes, or uses such dynamic proxies as reflection, Gclib, and so on to generate a class, it can cause a memory overflow in that area. 4. Thread Stack Overflow ( Java.lang.StackOverflowError )
Line stacks is a thread-specific piece of memory structure, so a thread stack problem must be the error that occurs when a thread runs.
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; to avoid memory leaks, you can refer to the following suggestions as you write your code:
1. Release references to useless objects as early as possible
2, using string processing, avoid the use of string, should use a large number of StringBuffer, each string object has to occupy a single area of memory;
3, to minimize the use of static variables, because static variables exist in the permanent generation (method area), the permanent generation basically do not participate in garbage collection;
4, avoid creating objects in the loop;
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.

Java Virtual machine-Memory Overflow exception

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.