Java Virtual machine--JVM memory structure

Source: Internet
Author: User

What is 1.JVM?

  Jvm:java virtual mechinal (Java vm). It is a fictional computer that is implemented by simulating various functions on a real computer. The main task of the JVM is to interpret its own set of instructions (bytecode, such as Java source code compiled into a class file to run on a virtual machine) and map to a local CPU instruction set or system call to the OS. The essence of the cross-platform of Java languages is that different operating systems use different JVM mapping rules to make them independent of the operating system, thus enabling cross-platform.

What does the 2.JVM memory structure look like?

  When a Java virtual machine runs a Java program, it divides the memory it manages into a number of different data regions:

      

3. So what does each data area do next?

  (1). Program counter: a small amount of memory space that can be viewed as the line number indicator of the byte code executed by the current thread. Especially in the case of multithreading, especially important. The multithreading of a Java Virtual machine is achieved by rotating threads and allocating processor execution time, that is, at any given moment, a processor executes only one thread, and when the thread switches, it needs to be restored to the correct location, so the program counter is thread-isolated. Each thread has its own dedicated counter. It is important to note that this memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.

  (2). Heap : This memory area is the largest chunk of memory managed by a Java virtual machine and is common to threads, which are created when the virtual machine is started. It is used to store object instances in Java (regardless of member variables, local variables, or class variables, all of which are stored in heap memory), where almost all object instances allocate memory. It is also the main area of GC. From the memory recycling point of view, because the collector basically uses the generational collection method, so in the Java heap can also be subdivided into: the new generation and the old generation (can be understood as the object memory location is different), and then detailed can be divided into:Eden Space, from Survivor space, to Survivor Space (8:1:1). From the memory allocation point of view, the thread-shared Java heap may divide multiple thread-private allocation buffers. The purpose of partitioning is to better reclaim memory or to allocate memory more quickly.

Note: The Java heap can be in a physically discontinuous memory space as long as it is logically contiguous.

(3). Virtual Machine Stack :   It is thread-private, with the same life cycle as the thread. Three parts: local variable area, operand stack, frame data area. (Details will be introduced later in this article)

   (4). Local method Stack : similar to the virtual machine stack, the difference is that the former performs the native method service for the virtual machine, which executes the Java method service for the virtual machine. But the implementation of different virtual machines is different, such as the Sun hotspot virtual machine directly into the two stacks.

   (5). Method Area : A thread-shared memory region that stores the structure information of a Java class. when we create an object instance, the object's type information is stored in the method area, the instance data resides in the heap, the instance data refers to the various instance objects created in Java and their values, and the type information refers to the constants defined in Java code, the static variables, and the various methods declared in the class, Method fields, and may include code data generated by the immediate compiler compilation.

4. In-depth understanding of the stack:

(1). Let's summarize the differences between heaps and stacks:

①. Different functions: stack memory is used to store local variables and method calls, while heaps are used to store object instances.

②. Sharing is different: the stack is thread-private, while the heap is common to threads.

    ③. Exception error: stackoverflowerror when stack space is low, outofmemoryerror when heap space is low.

    ④. The size of the space is different: the heap is much larger than the stack's memory size.

(2). We all know the three parts of the stack: the local variable area, the operand stack, and the frame data area.   where the local variable area and the operand stack depends on the corresponding method size, it is calculated by the word length. But when a method is called, it gets the local variable area and operand stack size from the type information and allocates the stack memory and presses it into the stack.

    Local variable area:

      is an array of bytes, starting from 0, the values of type short, Byte, char are converted to an int value before they are stored in the array, long and double are two consecutive bytes, but when you access long and double, you only need to remove the first successive index. (It can be understood that a local variable in a method accounts for different sizes of memory depending on the type, and has its own index subscript, refrence takes one byte)

    Operand stack:

      The same as the local variable area, which is the array in bytes. But the difference is that it is not accessed through the index, but through the stack and into the stack. The operand stack can be understood as a place to store temporary data when computing.

    Frame Data area:

      In addition to the two sections above, Java stack frames require some data to support constant pool parsing, normal method return, and exception dispatch mechanisms. This data is stored in the frame data area. When the JVM executes to data that requires a constant pool, it accesses it through a pointer to a constant pool in the frame data area. In addition, the data in the frame data area also handles the normal end of the Java method and the exception termination: If the current stack frame is loaded by return, the value is pressed into the stack of the operation that originated the calling method, if there is a return value, and if the exception is aborted, a reference to this method exception reference table is saved in the frame. When there is an exception, the JVM is looking for code in the catch code block, and if none is immediately aborted, the information in the frame resumes the frame of the method that originated the call, and then the context of the calling method throws the exception again.

5. Note the point:

(1).   the basic data types in Java are not necessarily stored in the stack. The amount of memory used to store local variables and method calls, if the local variable is the base data type, is stored in the stack, and if the local variable is an object, it is stored in the heap.

(2).   The method area, called "permanent generation", is not equivalent in nature, just because the GC is extended to the method of collection. The purpose of memory reclamation in this area is primarily to recycle the constant pool and unload the type (memory data), but the recovery efficiency is very low. A run-time constant pool is a part of the method area used to store symbolic information in a constant pool of Java class files.

(3).   HotSpot Method Area changes: jdk1.2-6, using the permanent band implementation method area, using GC generational implementation method area; JDK7 Oracle HotSpot removes the permanent generation, and the symbol table in JDK 7 is moved to the native heap. string constant pools and class references are moved to the Java heap, and JDK 8 permanent generations have been completely superseded by the Meta space.

Java Virtual machine--JVM memory structure

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.