JVM interpretation (III): JVM memory region, interpretation of jvm memory region

Source: Internet
Author: User

JVM interpretation (III): JVM memory region, interpretation of jvm memory region

The full name of JVM is java Virtual Machine (java Virtual Machine). JVM shields software and hardware differences related to various computer platforms.
In the following days, I learned JVM by writing a blog so that I can better understand Java!
This series of articles summarizes "deep analysis of Java Web technology insider" and "deep understanding of Java Virtual Machine". You are welcome to join us and make progress together.
JVM interpretation Article 1: JVM Architecture
JVM interpretation Article 2: JVM Class Loader ClassLoader
JVM interpretation Article 3: JVM memory Zone
JVM interpretation Article 4: JVM memory overflow exception Analysis
JVM interpretation Article 5: JVM garbage collection

One of the major advantages of java that everyone knows is that it does not need to be displayed by programmers to allocate and recycle memory. This is because the Automatic Memory Management Mechanism of virtual machines helps us deal with all this. Next let's take a look at how the Virtual Machine divides the memory.
The memory area divided by the Java Virtual Machine is called the runtime data area. These regions are called runtime data regions only after JVM is started. Let's take a look at a classic image.

Program counters

The program counter is a small memory space and can be seen as the row number indicator of the bytecode executed by the current thread. The multi-thread of a virtual machine is implemented by switching threads in turn and allocating the execution time of the processor. at any specified time, a single processor will only execute the commands in one thread. In order to restore the thread to the correct position after switching, each thread requires an independent program counter, which has no mutual influence, independent storage, and the thread is private.
If the thread is executing a java method, this counter records the address of the Virtual Machine bytecode command being executed. If the local method is executed, this counter is empty.

This memory region is the only region that does not specify any OutOfMemoryError conditions in the Java Virtual Machine specification.

Virtual Machine Stack

Every time a method is executed, the elements of the stack frame will pop up as the return value of this method, and the stack frame will be cleared. The stack top of the java stack is the currently executed active stack, this method is also being executed, and the address in the PC register also executes this method. Only the local variables of the active stack can be used by the Operation stack. When another method is called in the stack frame, a new stack frame corresponding to the method is created, this new stack frame is placed on the top of the stack and becomes an active stack frame. When all the commands in this stack frame are executed, this stack frame is removed from the stack, the stack frame is also called an active stack frame. The returned value of the previous stack frame is changed to an operand in the Operation stack of this stack frame. If no value is returned, the Operation stack operations of the current stack frame remain unchanged.

Heap

Heap is the place where java objects are stored and the core area where JVM manages java objects. Each java object stored in the heap is a copy of this object class, it copies all non-static attributes inherited from the parent class. The heap is shared by all java threads. Pay attention to the synchronization problem when accessing the heap. The methods and attributes must be consistent.

Each java application corresponds to only one JVM instance, and each instance corresponds to only one heap. All the class instances or arrays created by the application during running are stored in this heap and shared by all the threads of the application. In java, heap memory allocation is automated. The memory space of all objects is allocated in the heap, but the reference of this object is allocated in the stack. Memory is allocated in both places when an object is created. The memory allocated in the heap actually creates this object, the memory allocated in the stack is just a pointer (reference) pointing to the heap object.

Method Area

The JVM method area is used to store class structure information. A class file is parsed into several other parts that the jvm can recognize. When the class is loaded to the JVM, it is stored in different data structures, where the constant pool, domain, method data, method body, constructor, private method in the class, instance initialization, and interface initialization all exist in this region.
In fact, the method area also belongs to the heap area, which is usually called the Permanent Generation in the java heap. This area is shared by all threads, this region is usually fixed within a period of time after the program starts. After the JVM runs for a period of time, the classes to be loaded are usually loaded into the JVM.
The special point of the method area is that it is not frequently recycled by the GC recycler as the java heap, and its storage information is relatively stable. But it exists in the java heap and will still be managed by the GC recycler.

Runtime constant pool

The runtime constant pool is part of the Method Area. In addition to the version, field, and method information of the class file, there is also a constant pool, it is used to store various literal and symbolic references generated during the compilation period. This part of content will enter the runtime frequent pool in the method area after the class is loaded.

Local method Stack

The local method stack is the space for JVM to run local methods, which is similar to the java stack.
The preceding figure shows the partitions in the Data zone when the VM is running. The following is a brief introduction.Direct Memory. Direct Memory is not part of the VM runtime data zone, nor is it part defined in the Java VM specification. However, this part of memory is frequently used, which also causes an OutOfMemoryError error.
After java1.4, the NIO (New Input/Output) class is added, and an I/O Method Based on channels and buffers is introduced. You can directly use the local function library to directly allocate off-heap memory, then, use a DirectByteBuffer object stored in the java heap to reference the latest memory. This can significantly improve performance, because it avoids copying data back and forth between the java heap and Native heap.

Comparison Between Stack and stack

  • The stack is mainly used to store objects, while the stack is mainly used to execute programs.
  • The advantage of heap is that the memory size can be dynamically allocated without the compiler control for the lifetime.
  • The data size in the stack is related to the lifetime and lacks flexibility.
  • The heap memory is dynamically allocated with good flexibility, but this also leads to a slow access speed.

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.