Java Common Memory Overflow (OOM)

Source: Internet
Author: User
Tags xms

JVM Memory Area
    1. Program counter
      A small amount of memory space that is the line number indicator of the byte code executed by the current thread.
    2. Java stack
      As with program counters, the Java stack (the virtual machine stack) is also thread-private, with the same lifecycle as the thread. Typically holds the base data type, the object reference (a reference pointer to the start address of the object, or a handle to the object), the Reeturnaddress type (the address of a bytecode directive)

      There are two types of exceptions in the stack area: if the thread requests a stack depth greater than the virtual machine allows, the STRACKOVERFLOWERROR exception will be thrown, and if the virtual machine stack can be dynamically extended (most virtual machines can be dynamically extended), An OutOfMemoryError exception is thrown when an extension fails to request enough memory.

    3. Local method Stack
      Similar to the virtual machine stack, the difference is that the virtual machine stack executes Java method services for the virtual machine, while the local method stack serves the native method used by the virtual machine. As with virtual machine stacks, stackoverflowerror and OutOfMemoryError exceptions can be thrown.
    4. Java heap
      The Java heap is the largest area of memory managed by the JVM. Javaheap is a piece of memory that is shared by all threads and created when the virtual machine is started. The primary storage object instance. Javaheap is the main area of garbage collector management, which can be subdivided into the new generation and the old age. A OutOfMemoryError exception is thrown if there is no memory in the heap to complete the instance assignment and can no longer be extended.
    5. Method area
      Like Javaheap, the memory area shared by each thread is used to hold data such as class information, constants, static variables, and code compiled by a timely compiler that have been loaded by the virtual machine. The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements. The method includes frequently-heard run-time pools, which are used to hold various literal and symbolic references generated during the compilation period.
    6. Direct Memory
      Direct memory is not part of the data area when the virtual machine is running, nor is the memory area defined in the Java VM Specification, which is an area of memory outside the JVM, which can also cause outofmemoryerror exceptions.
JVM parameters

-XSS (stackspace) stack space

-XMS,-xmx (heap memory space) heap space: Heap is the most familiar area, he is the area used by the JVM to store object instances, the heap in a 32-bit system with a maximum of 2G, its size is controlled by-XMS and-xmx,- XMS is the minimum heap memory requested by the JVM when it starts, default to 1/64 of physical memory, but less than 1g,-xmx is the maximum heap memory that the JVM can request, default to 1/4 of physical memory, and generally less than 1G, by default when free heap memory is less than 40%. The JVM will have a maximum heap size of-xmx specified size, which can be specified by-xx:minheapfreeratio, and when the free heap memory is greater than 70%, the JVM will resize the heap to a size specified by-XMS, via-XX: Maxheapfreeratio to specify this scale, but usually to avoid frequent resizing of heapsize, set-XMS and-XMX values to be the same.

-xx:permsize-xx:maxpermsize: Method Area Persistent Generation size: The method area is also globally shared, and under certain conditions it is also GC, and when the method area needs to use more memory than it allows, it throws outofmemory error messages.

Common Memory Overflow Error resolution
  1. OutOfMemoryError exception
    In addition to the program counters, several other runtime areas of the virtual machine memory have the possibility of OutOfMemoryError (OOM) exceptions.

    Java Heap Overflow

    General exception information: Java.lang.OutOfMemoryError:Java heap Spacess

    The Java heap is used to store object instances, as long as we constantly create objects, and ensure that GC roots to objects with an attainable path to avoid the garbage collection mechanism from clearing these objects, a memory overflow exception occurs after the number of objects reaches the maximum heap capacity limit.

    In this case, the general approach is to first analyze the dump snapshot with a memory image analysis tool (such as Eclipse Memory Analyzer), with a focus on confirming that the object in memory is necessary, first distinguishing it from the memory leak Leak Memory Overflow.

    In the case of a memory leak, further tools can be used to view the reference chain of the leaking object to the GC roots. It is then possible to find out what path is associated with GC roots when leaking objects and causes the garbage collector to not be automatically recycled.

    If there are no leaks, you should check that the settings for the virtual machine's parameters (-XMX and-XMS) are appropriate.

  2. Virtual machine stack and local method stack Overflow
    A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the maximum allowed depth for the virtual machine.

    Throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space in the expansion stack

    It is important to note that the larger the stack size, the less the number of threads can be allocated.

  3. Run a constant pool overflow
    Exception information: Java.lang.OutOfMemoryError:PermGen space

    The simplest way to add content to a running constant pool is to use the String.intern () native method. The function of this method is that if the pool already contains a string equal to this string, a string object representing the string in the pool is returned, otherwise the string contained in this string object is added to the constant pool, and a reference to this string object is returned. Because the constant pool is allocated within the method area, we can limit the size of the method area through-xx:permsize and-xx:maxpermsize, thereby indirectly restricting the capacity of the constant pool.

  4. Method Area Overflow
    The method area is used to store information about class, such as its name, access modifier, Chang, field description, method description, and so on.

    Exception information: Java.lang.OutOfMemoryError:PermGen space

    Method area Overflow is also a common memory overflow exception, and a class is very demanding if it is to be reclaimed by a garbage collector. This is especially important in applications that generate a large number of classes on a dynamic basis.

Reference

"In-depth understanding of Java virtual machines"

Java Common Memory Overflow (OOM)

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.