Java Virtual machine Memory overflow various scenarios summary

Source: Internet
Author: User

Java Heap Overflow

The Java heap is used to store object instances, as long as the objects are constantly created and the GC roots to the object to avoid the garbage collection mechanism to clear these objects, then the object reaches the maximum heap capacity limit, resulting in a memory overflow overflow.

Exception: Java.lang.OutOfMemoryError:java heap Space

To solve the anomaly in this area, first distinguish between memory Leak or memory overflow (OverFlow). Workaround: If it is a memory leak, use the tool (Eclipse Memory Analyzer) to view the reference chain of the leaked object to the GC roots. You can then find out what path the leaked objects are associated with GC roots and cause the garbage collector to not automatically reclaim them. Having mastered the type information of the leaking object and the information of the GC roots reference chain, we can find out the location of the leaking code accurately. If there is no leakage, in other words, the objects in memory must still exist, then you should check the virtual machine's heap parameters (-xmx and-XMS) and machine physical memory can also be adjusted, from the code to check if there are some object life cycle is too long, hold the state of the situation too long, Try to reduce the memory consumption during program run time.

Virtual machine stack and local method stack Overflow

-xoss parameter Set local method stack size-XSS parameter set stack capacity

The-xoss parameter is valid, depending on which virtual machine the JVM uses, such as the-xoss parameter (invalid) if the hotspot virtual machine is used, so that the virtual machine stack and the local method stack are controlled by the stack capacity.

With regard to the virtual machine stack and the local method stack, two exceptions are described in the Java Virtual Machine specification: A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the maximum allowed depth of the virtual machine. Throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space on the scale stack.

Under a single thread, either because the stack frame is too large or the virtual machine is too small, the virtual machine throws a Stackoverflowerror exception when the memory is not allocated. If you are not limited to single threads, in this case, the larger the memory allocated for each thread's stack, the more likely it is that the memory overflow exception is generated.

If multiple threads are established to cause a memory overflow, the number of threads can only be exchanged by reducing the maximum heap and reducing the stack capacity in the case of a failure to reduce the thread count or replace a 64-bit virtual machine.

Method area and run-time-constant pool overflow

Running a constant pool is part of the method area. Starting from JDK1.7, we discuss version 1.6, in version 1.6, because the constant pool allocation is within the permanent generation, we can limit the method area size by-xx:permseize and-xx:maxpermseize, thereby indirectly limiting the capacity of the constant pool.

Exception: Java.lang.OutOfMemoryError:PermGen space

The method area is used to store class-related information such as class name, access modifier, constant pool, field description, method description, and so on. Method area anomaly is a common memory overflow exception, a class to be reclaimed by the garbage collector, the criterion is more stringent. In applications that generate large numbers of classes on a regular basis, special attention needs to be paid to the recycling of the class.

Native Direct Memory Overflow

Exception: Java.lang.OutOfMemoryError

Directmemory capacity is available through-xx:maxdirectmemorysize, and if not specified, the default is the same as the Java heap maximum (-xmx specified). Memory overflow caused by directmemory, an obvious feature is that the heap dump file will not see the obvious exception, if you find that the dump file is small, and the program directly or indirectly using NIO, then you can consider whether this is the reason.

Java Virtual machine Memory overflow various scenarios summary

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.