<< deep Java Virtual Machine >>-chapter II-java Memory area-Learning notes

Source: Internet
Author: User

Java run-time memory area

When running a Java program, a Java Virtual machine divides the area of memory it manages into several different regions. Each zone has its own purpose, creation, and time to destroy. Some exist with the startup of the virtual machine, and some rely on the user thread to start and destroy.

  1. Program counter

    A program counter is a small area that can be seen as a line number indicator used to represent a line that a thread executes to a byte code.

    In a Java virtual machine, multithreading is implemented by switching between threads and allocating the execution time of the processor, so that each thread has a "thread-private" program counter, so that it can continue to go back to the previous line of code after the thread switch.

    If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed. If you are performing a native method, this counter is empty.

  2. Java Virtual Machine stack

    The Java Virtual machine stack is also thread-private, with the same life cycle as the thread.

    Each method creates a stack frame when it is executed to store information such as local variable tables, operation stacks, dynamic links, method exits, and so on.

    Each method is invoked until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.

  3. Local method Stack

    The local method stack is similar to the virtual machine stack, where the virtual machine executes the Java method service for the virtual machine, and the local method stack serves the native method used by the virtual machine.

    The Sun hotspot virtual machine is directly combining the local method stack with the virtual machine stack.

  4. Java heap

    The Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine.

    The Java heap is an area that is shared by all threads and created when the virtual machine is started. The purpose of this area is to store object instances.

    Java heap can also be subdivided into: the new generation and the old age. Regardless of the partitioning, the storage is still an object, and the purpose of the division is to recover memory or allocate memory more quickly.

  5. Method area

    The method area, like the Java heap, is the area of memory shared by each thread.

    The method area is used to store data such as the information, constants, static variables, and immediately compiled code of the classes that have been loaded by the virtual machine.

    Virtual Machine garbage collection behavior is rare in this region, where the memory recovery target is primarily the recovery of constant pools and the unloading of types.

    • Run a constant-rate pool

      Running a constant pool is part of the method area. In addition to the class version, fields, methods, interfaces, and other descriptive information, there is also a constant pool of information, used to hold the compilation period to generate a variety of literal and symbolic references, which will be stored in the run-time pool after the class is loaded.

      An important feature of running a constant pool is that it is dynamic compared to the regular pool of class files. Constants are not necessarily generated only at compile time, and new constants can be put into the pool during run time, such as The Intern () method of the String class.

Direct Memory

Direct memory is not part of the data region when the virtual runtime is running. Direct memory occurs after Java joins NIO, and NiO introduces a channel-and buffer-based I/O approach that can consume native libraries to directly allocate out-of-heap memory. It is then manipulated by a Directbytebuffer object stored in the Java heap as a reference to this memory.

Object access

For Object obj = new Object ();

Object obj will be reflected in the Java stack's local variable table, as a reference type of data appears. The new object () is then reflected in the Java heap, forming a piece of structured memory that stores all instance data values of type object. The Java heap must also contain address information that can be traced to this object type data (such as object types, parent classes, implemented interfaces, methods, and so on), and these types of data are stored in the method area.

In the Java Virtual Machine specification, the reference type is a reference to an object, and different virtual machine implementations have different ways of referencing the object, and there are two main ways of accessing it: using a handle and a direct pointer.

      • Using handles

        In the Java heap, a piece of memory is partitioned as a handle pool, where the handle address of the object is stored in the reference, and the handle contains the specific address information of the object's instance data and type data.

      • Direct pointer

        With direct pointer access, the address of the object is directly stored in the reference.

Advantages and disadvantages of two access methods:

The biggest benefit of using handle access is that the reference holds a stable handle address and only needs to modify the instance data pointer in the handle when the object is moved (the behavior that is very common when the object is moved when it is garbage collected), and the reference itself does not have to be modified.

The biggest benefit of using direct pointer access is faster speed, which saves time spent in the middle of the pointer positioning, and access to objects is very frequent in Java, and this overhead is a significant execution cost.

Unlike the virtual machines of each vendor, the Sun hotspot virtual machine uses direct pointer access to object access.

<< deep Java Virtual Machine >>-chapter II-java Memory area-Learning notes

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.