Deep understanding of JVM memory zone and memory overflow

Source: Internet
Author: User
Tags xms

Deep understanding of JVM memory zone and memory overflow

Document directory

  • 1.2. HotSpot Virtual Machine

  • 1.3. troubleshooting of OOM exceptions

  • 1.4. Reference

  • Java memory overflow exception

    Data Area During Running

    Program counters

    • Row number indicator of the bytecode executed by the current thread

    • The current thread is private

    • There will be no OutOfMemoryError

    Java Virtual Machine Stack

    • The thread is private and has the same lifecycle as the thread.

    • Memory Model for java method execution. A stack frame is created when each method is executed to store information such as the local variable table (basic type, Object Reference), operand stack, dynamic link, and method exit.

    • StackOverflowError exception: When the stack depth requested by the thread is greater than the depth allowed by the Virtual Machine

    • OutOfMemoryError exception: If the stack cannot be extended, enough memory is applied.

    Local method Stack

    Similar to the Virtual Machine stack, the Native method service is mainly used by virtual machines. In the HotSpot virtual machine, the local method stack is directly integrated with the Virtual Machine stack.

    Java Heap)

    Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this region is to store object instances. Java heap is the main area for managing the garbage collector. Java heap can also be subdivided into the new generation and old generation. The details include Eden space, Form compute vor space, and To compute vor space.

    • You can use-Xmx and-Xms to control the heap size.

    • OutOfMemoryError exception: when no memory in the heap completes instance allocation and the heap cannot be expanded.

    Method Area

    • Thread sharing

    • Stores information about classes loaded by virtual machines, constants, static variables, Code Compiled by the real-time compiler, and other data.

    • OutOfMemoryError exception: when the method area cannot meet the memory allocation requirements

    Runtime constant pool

    • Part of the Method Area

    • Used to store various literal and symbolic references generated during the compilation period

    • OutOfMemoryError exception: When the constant Pool cannot be applied to the memory again

    Direct Memory

    • NIO can use the Native function library to directly allocate off-heap memory. The DirectByteBuffer object in the heap is used as a reference for this memory.

    • The Java heap size is not limited by the local (server) memory size.

    • OutOfMemoryError exception: when the system memory is insufficient

    HotSpot Virtual Machine

    Object Creation

    When the VM encounters a new command, it first checks whether the parameter of this object is located in the constant pool as a symbol reference of a class, check whether the class referenced by this symbol has been loaded, parsed, and initialized. If no, the class loading process must be performed first.
    After the class loading check is passed, the VM allocates memory for the new object. The memory size required by the object can be determined after the class is loaded. Memory allocation can be implemented by means of "pointer Collision" and "Idle list.

    Object Access and positioning

    Java programs need to use the reference data on the stack to operate on specific objects on the stack. There are two access methods: handle and direct pointer.

    • When a handle accesses the java heap, a memory block is allocated as the handle pool. The reference stores the handle address of the object, the handle contains the specific address information of the object instance data and type data.

    • In the layout of direct pointer access to java heap objects, you must consider how to place information related to access type data. The object address stored in reference is the object address.

    Troubleshooting of OOM exceptions

    Generate a Dump snapshot file:

    • The jvm parameter-XX:-HeapDumpOnOutOfMemoryError allows the JVM to Dump the current memory Dump snapshot when memory overflow occurs.

    • Use jmap to generate dump files, win view the tomcat process pid through the task manager, linux use the ps command to view the process pid, and then use the jmap command

    First, use the Memory Image Analysis Tool (such as the Memory Analyzer of Eclipse) for analysis. common situations include:

    • Memory leakage, the object is dead, and automatic recycle cannot be performed through the garbage collector. The solution can be determined by finding out the location and cause of the leaked code;

    • Memory overflow. All objects in the memory must be stored. This indicates that the Java Heap has insufficient space to allocate. Check the heap settings-Xmx and-Xms ), check whether the Code contains a situation where the object's lifecycle is too long and its holding status is too long.

    OOM exception example:

    Package oom;

     
     
    1. import java.util.ArrayList; 
    2. import java.util.List; 
    3.  
    4. /** 
    5. * VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError 
    6. * @ClassName: HeapOOM 
    7. */ 
    8. public class HeapOOM { 
    9.     static class OOMObject{ 
    10.  
    11.     } 
    12.  
    13.     public static void main(String[] args) { 
    14.         List<OOMObject> list = new ArrayList<OOMObject>(); 
    15.         while(true){ 
    16.             list.add(new OOMObject()); 
    17.         } 
    18.     } 

    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.