Deep understanding of JVM virtual Machine reading notes-java memory area and memory overflow exception

Source: Internet
Author: User
1. Run-time data area

1.1 Program Counters

The program counter is a thread-isolated area where each thread has a separate program counter.

It is a small amount of memory space, the bytecode interpreter by changing the counter to select the next byte code instruction to execute, branch, loop, jump, exception handling, thread recovery and other basic functions need to rely on it to complete.

1.2 Java Virtual Machine stack

The Java Virtual machine stack is also thread-isolated, with the same lifecycle and thread.

The virtual machine stack is the memory model that the Java method executes, and each method executes creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and every method from the call to the completion of the execution. Corresponds to a stack frame in the virtual machine stack into the stack to the process.

Two exceptions: If the thread requests a stack depth greater than the virtual machine allows, the Stackoverflowerror exception is thrown, and when the virtual machine stack dynamically expands, it cannot request enough memory, and the OutOfMemoryError exception is thrown.

1.3 Local Method Stack

The local method stack acts similarly to the virtual machine stack. The difference is that the virtual machine performs the Java method service for the virtual machine, and the local method stack serves the native method used by the virtual machine.

1.4 Java Heap

For most applications, the Java heap is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated memory.

You can control the size of the heap through-xmx and-XMS, and in the console input java-x can see what they mean,-xmx is the maximum value of the set heap,-XMS is the size of the heap initialization. The OutOfMemoryError exception is thrown when the team does not complete the memory allocation and can no longer expand.

1.5 Method Area

The method area is also an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine.

Java8 later put it in native memory. So what is native memory? Thanks for the memory, Linux

1.6 Running a constant-rate pool

Running a constant pool is part of the method area.

It is used to hold various literal and symbolic references generated during the compilation period, which will be stored in the run-time pool of the method area after the class is loaded into the method area.

Note that constants are not necessarily compile-time, but can also be used for example, the Intern () method of the String class. An oom is thrown when the memory cannot be requested.

1.7 Direct Memory

jdk1.4 added to the NIO class, you can use the native library to directly allocate out-of-heap memory and then manipulate it through a Directbytebuffer object stored in the Java heap as a reference to that memory.

Direct memory is not limited by the Java heap size, but it receives the limit of the total memory and processor addressing space for this level.

2 Virtual Machine Objects Quest 2.1 Creation of objects

After encountering the new instruction, the virtual machine checks to see if there is a symbolic reference to the class in the constant pool, and checks whether the class is loaded, parsed, and initialized by the symbol reference, and if not, it must first execute the class's loading process (described later).

The amount of memory required for an object can be fully determined after the class is loaded.

There are two ways to allocate memory, one is "pointer collisions," which are used when the heap memory is absolutely normalized. One is the idle list, which is used when the heap memory is not regular. The Java heap is structured and is determined by whether the garbage collector is using compression.

2.2 Memory layout of objects

The layout of objects stored in memory can be divided into 3 areas: object header, instance data, and padding.

The object header has two parts, one for storing run-time data for the object itself, such as hashing, GC generational age, and so on. This part of the data in 32-bit and 64-bit virtual machines are asked 32bit and 64bit respectively. The other part is the type pointer, which is the pointer to its metadata, which the virtual machine uses to determine which class of the object you are an instance of.

The next instance data section is a valid piece of information that is actually stored, as defined in both the parent class and the subclass needs to be recorded.

The third part of the object population does not necessarily exist, only the role of the placeholder, because the hotspot VM's automatic memory management system requires that the object's starting address must be an integer multiple of 8 bytes.

2.3 Object access and positioning

There are two ways to access the current mainstream: using handles and direct administration.

The advantage of using a handle is a stable handle address, where the object moves only with the instance data pointer in the change handle, and the maximum benefit of the pointer is fast. Hotspot is the second type.

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: