Deep understanding of Java Virtual Machine-Reading Notes (1): JVM runtime data area

Source: Internet
Author: User
Document directory
  • Program counters
  • Java Virtual Machine Stack
  • Local method Stack
  • Java heap
  • Method Area
  • Runtime constant pool
  • Direct Memory

This series provides Reading Notes for deep understanding of Java Virtual Machine (by Zhou Zhiming.

Java programmers generally know that there are stacks and stacks in the JVM, and they simply understand that objects are allocated on the stack. This is easily thought of by programmers who switch from C/C ++ to Java. However, due to its own features, Java also has some other memory areas, as shown in:

The program counter does not occupy much memory space. It records the row number of the current bytecode of each thread. JVM multithreading is implemented by rotating the CPU time. Therefore, when a thread leaves its CPU slice, it needs to record the current status (bytecode line number ). To prevent the interaction between threads, each thread has its own program counter. This type of memory area is also called "thread private" memory. For example, the VM stack, local method stack, and program counter all belong to the "thread private" memory. If the program executes a Java method, the program counter records the bytecode address of the current thread; if the program executes a local method, the program counter is blank (undefined ). Java programmers often say "stack" and "stack". The "stack" refers to the Java Virtual Machine stack. It can be seen that, like program counters, the Java Virtual Machine stack is also proprietary to the thread, and the lifecycle is the same as that of the thread. The Virtual Machine stack stores stack frames, including local variable tables, operand stacks, dynamic links, and returned information. The local variable table stores various basic data types (Boolean, byte, Char, short, Int, float, long, double), object references, and ruturnaddress types known during the compilation period. From this we can see that the use of integer and INT in memory space is different. In 32-bit machines, integer occupies a reference (8 bytes) + an int (4 bytes), while int occupies only 4 bytes of space. In the local variable table, the long and double values of 8 bytes occupy two local variable spaces (slots), and the other types only occupy one. The local method stack is similar to the Java Virtual Machine stack in terms of structure and usage. The difference is that the local method stack executes the native method, and the Java (bytecode) method is used in the Java Virtual Machine stack) method. Java heap is an area shared by all threads. It is created when a virtual machine is started. This area is mainly used to store object instances. Almost all object instances are allocated in the heap. Java heap is the main area of GC management. Because many garbage collectors adopt the "generational Collection" policy, Java heap can also be subdivided into the new generation and the old generation. The JVM specification specifies that Java heap only needs to be logically consecutive, and whether the Java heap is physically consecutive is irrelevant. The method area is the same as the Java heap, and is shared by multiple threads, stores information about classes loaded by virtual machines, constants, static variables, Code Compiled by the real-time compiler, and other data. Relatively speaking, the memory in this region rarely needs to be recycled. The main purpose of this region is to recycle the constant pool and unload the type. The runtime constant pool is a part of the method area. In addition to descriptive information such as the class version, field, method, and interface, the class file also contains a constant pool for storing various types of literal and symbolic references generated during compilation, this part of the content will be stored in the method zone runtime pool after the class is loaded. Another important feature of the constant pool of class files is dynamic. Java does not require constants to be generated only during compilation, that is, it is not only the content of the constant pool preset into the class file that can enter the runtime constant pool in the method area. During the runtime, new constants may also be placed in the pool. Direct Memory direct memory is not part of the Virtual Machine runtime data zone, nor is it part of the Java Virtual Machine specification, but this part is often mentioned in NIO. NIO is based on channel and buffer. It can use the native function library to directly allocate off-heap memory, then, a directbytebuffer object stored in the Java heap is used as a reference to the memory. This can significantly improve the performance in some scenarios, because it avoids repeated data replication in the Java and native stacks.
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.