Deep understanding of Java Virtual Machines-Virtual machine memory partitioning

Source: Internet
Author: User
Tags xms

in memory management, Java differs from C and C + + in that Java has dynamic memory allocation and garbage collection technology, but usually we seldom pay attention to the memory structure of the JVM and the GC, in the event of a memory leak or overflow problem, the troubleshooting work will become extremely difficult.

     1. Run-time data region    Java Virtual machines perform Java programs by dividing the memory they manage into several different data regions that have their own different lifecycles. According to the Java Virtual Machine specification, the memory managed by the Java Virtual machine contains the following areas. It can be divided into shared memory area and two parts of the thread isolation data area.        2. Program counter    The program counter is a very small memory space that can be seen as the line number indicator of the bytecode that is being executed by the current thread. In the virtual machine, the execution of the program, jump, loop, etc. all need the value of this counter to select the next command to execute. Program counters occupy a very small amount of memory, and usually do not appear oom or SOF.    However, it is important to note that the program counter, if it is executing a Java method, holds the address of the next instruction, or null if the Natvie method is being executed.    3. JAVA Virtual Machine stack    Like the program counter, the virtual machine stack is also thread-private, and its cycle is the same as the thread's life cycle, which describes the memory model that the Java method executes: Each method executes at the same time creating a stack frame for storing local variable tables, operation stacks, dynamic links, method exits, and so on. Each method is called until the completion process corresponds to a virtual machine stack from the stack to the stack.    Java memory area is generally used to be divided into heaps and stacks, this partitioning method is actually relatively coarse, when the so-called stack is the virtual machine stack.    The local variable table holds the basic data types, object references, and returnaddress types that are known at compile time.    It is important to note that the memory space required for the local variable table is allocated during the compilation period, and when entering a method, how much memory space is allocated in the frame is fully deterministic and does not change at run time.    In the Java Virtual Machine specification, two exceptions are defined: If the thread request depth is greater than the virtual machine allowed depth, the Stackoverflowerror exception is thrown and the OutOfMemoryError exception is thrown if the extension cannot request enough memory.    Set stack size by-XSS
// size of the virtual machine stack and local method stack = thread allows maximum memory-maximum heap capacity-Maximum method area capacity // in multi-threading, the larger the stack allocated to each thread, the more prone to exceptions
    4. Local Method Stack    The local method stack serves only the native method used by the virtual machine, and other similar to the virtual machine stack    5. Heap    The Java heap is the largest part of Java memory management, which is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this memory area is to hold an instance of the object.    The Java heap is the primary area of the GC and is sometimes referred to as a "GC heap." The Java heap can also be subdivided: the new Generation and the old: Another detail can be divided into Eden space, from Survivor space, to survivor space and so on. Although it is divided into multiple spaces, it is still the object, which is designed to reclaim memory better and faster.    The Java heap can be located on a discontinuous memory space and can be configured to extend-XMX and-xms control, which throws a "OutOfMemoryError" exception if memory allocations are not completed in the heap and cannot be extended.    Example:
// Heap Overflow // -xmx Heap Maximum value // -xms Heap Minimum value // -xx:+heapdumponoutofmemoryerror Setting the virtual machine dumps the current memory heap on exception // You can use Eclipse Memory Analyzer to analyze a dump heap
    6. Method Area    The method area is also a thread-shared memory area used to store loaded classes, constants, static variables, and immediate compiler-compiled code.    For developers using the hotspot virtual machine, it is often referred to as a permanent generation, meaning that there is less garbage collection in the method area, mainly the cleanup of constants and unloading of classes, but the effect of reclaimed memory is worse than that of other memory regions.    7. Run a constant-rate pool    Runtime constant pool is part of the method area, where literal and symbolic references are saved in the class file, and the constant pool can also import data at run time    8. Direct Memory    Direct memory is not part of the virtual machine's running memory and is a memory area of the running environment.    NIO, introduced in Java, can directly manipulate native memory, although it is no longer constrained by the JVM heap size, but is still subject to the running environment memory limit, and when the timeout is exceeded, an Oom exception is thrown.    9. Object access    Object obj = new Object ()    Object obj will be reflected in the local variable table of the Java stack as a reference type data.    New Object will be reflected in the heap and allocated a heap of structured memory    The Java Access object may have direct access to the latter handle in two ways, where direct access is fast and the handle is slightly slower, but the handle holds a stable handle address.

Deep understanding of Java Virtual Machines-Virtual machine memory partitioning

Related Article

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.