In-depth understanding of Java Virtual Machine reading notes an automatic memory management mechanism

Source: Internet
Author: User

One, run-time data region

1, the program counter is the private space of the thread, each thread has. Java code is executed for the thread or native code, when Java code: the address of the virtual machine bytecode directive, and the native code: The Count value is undefined. There is no possibility of outofmemeoryerror.

2. The Java Virtual machine stack is a private space for threads, each with a thread. The portion of the local variable table in the stack is allocated during compilation, and the size of each local variable space is 32 for length. The depth of the stack is too long to throw a Stackoverflowerror exception. When the stack is extensible, the OutOfMemoryError exception is thrown if there is not enough memory.

3. The local method stack acts like a Java virtual machine stack, except that it serves the native method.

4. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. OutOfMemoryError exceptions may be thrown.

5. The method area is also an area of memory that is shared by all threads. The main storage class information, constants, static variables and timely compiler compiled code, and so on. Garbage collection is also required, mainly the collection of constant pools and the unloading of types. OutOfMemory exceptions may be thrown.

6. The run-time constant pool is part of the method area. The class file contains information such as the version of the classes, fields, and constant pools. Constant pools are used to hold various literal and symbolic references generated by the compiler, which are loaded into the run-time pool of the method area when the class is loaded. New constants can also be placed in the pool during run time, such as The Intern () method of the String class.

7. Direct memory is not part of the data area when the virtual machine is running, nor is it a defined memory area in the Java VM specification. OutOfMemoryError exceptions may be thrown. In NiO mode, the native library is used to directly allocate out-of-heap memory, which is manipulated by the Directbytebuffer object in the Java heap, which avoids the duplication between the Java heap and the native heap and improves performance.

Second, object access

1. There are two main ways to access objects: using handles and direct pointers.

Third, actual combat: OutOfMemoryError anomalies

1. Java Heap Overflow: Exception stack information is: Java.lang.OutOfMemoryError:Java heap space.

Workaround: Use the Memory analysis tool (Eclipse Memory Analyzer) to analyze the dump heap dump snapshot to determine if it is a memory leak or a memory overflow. If it is a memory leak: Use the tool to view the application chain of the leaking object to the GC roots. If it is a memory overflow: Check the virtual machine's heap parameters against the machine's physical memory, and check that some large objects in the code have a long life cycle.

2, virtual machine stack and local method stack overflow: stack frame too large and stack too many levels, when the memory can not be allocated when the Stackoverflowerror exception. A OutOfMemoryError exception is thrown when multithreading cannot build its own stack.

3. Run a constant pool overflow: The simplest way to add content to a const pool at run time is the String.intern () method.

4, Method area overflow: If there is a dynamic generation of the behavior of the class, may overflow.

5. Native Direct Memory overflow: The default size equals the maximum value of the Java heap.

In-depth understanding of Java Virtual Machine reading notes an automatic memory management mechanism

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.