Drill down into the Java memory area

Source: Internet
Author: User

The first time to publish essays, a little excitement, or get to the point of it.

Java code run is generally divided into the compilation period and run time. The compile period is responsible for compiling. Java (such as Student.java) into a. class file, and then during runtime through the class loading process (load, connect (validate, prepare, parse), initialize, use, unload), the class loading phase will be analyzed in the next section, which is not described here.

Let's take a look at the data area diagram of the Java Virtual Runtime:

    

     I. Thread-isolated memory space

      1. Program counter 

The program counter is a small piece of memory space. It serves as the role of the line number indicator in the virtual machine. The byte-code interpreter determines the next bytecode instruction by changing the value of the counter, whether the order, loop, branch, and other execution order requires a line number indicator to determine the currently running instruction address. In multi-threading, a processor may appear to be executing at the same time as multiple threads, but in fact it is due to the switchover of the CPU scheduler's slices (each time slice is shorter). If thread a executes part of the execution, the CPU scheduler switches the allocation time slice to thread B, at which point the program counter is required to record the last instruction address executed by thread A to ensure that the correct command location is performed when switching to thread a again. Each thread will have its own program counter, and the program counters do not affect each other.

      2.java Virtual Machine Stack

     The life cycle of the stack and thread is consistent, created and destroyed as the thread is created. The main storage in the stack is the local variable table, the operand stack, dynamic link and method export, the local variable table holds 8 basic data types and object references, where the long and double types are 64 bits, so in the local variable memory accounted for 2 slot slots (equivalent to two local variables of memory), The object reference is not the same as the object itself, depending on the virtual machine implementation, it may be a reference pointer to the start address of the object, or it may point to a handle representing the object or other location associated with the object, and the ReturnAddress type (the address of a bytecode directive). Thread executes each method of the time will create a stack frame, into the method into the stack, out of the way out of the stack, the method after the end of the stack frame destruction.

      3. Local Method Stack

     The local method stack and the virtual machine stack are similar, except that the virtual machine stack is intended for the Java method, and the local method stack is served for the native method (the native-modified method is generally platform-dependent and portability is not high).

     

    two . Thread-Shared memory space

1. Heap

A heap is an area that all threads can share, and almost all objects and arrays have this memory space, which is created as the virtual machine starts and is the primary memory that the GC garbage collector recycles.

Since the main storage is the object, first to explore how the object is created, when the virtual machine received a new command, the first check the parameters of the directive can find the corresponding symbol reference in the constant pool, and check whether the symbol reference has class loading, parsing, initialization, etc. If not, perform the class load process and then allocate memory for the created object after execution.

Then look at the object's memory layout, which is divided into object headers, instance data, and padding. Where the object header is divided into run-time data and type pointers. Runtime data includes hash code, GC generational age, lock status flag, thread-held lock, bias thread ID, and bias timestamp. A type pointer is a pointer to a class's metadata, which is the class information (Method,field, etc.) that resides in the method area, but not all objects have type pointers. In addition, the array is a special kind of object, and it also holds the data of the length of the array in the object cast, which is different from the general object, the object length is recorded in the class metadata of the type pointer, and the length information is not recorded in the class metadata of the array. The instance data is the valid information that actually stores the object. The last part is to populate it, because the hotspot VM automatically manages in-memory object start address must be 8-bit integer times, and the object header is exactly 8-bit integer times, but the instance data is not necessarily 8-bit integer multiples, and its fill is the instance data is filled, it is filled with 8-bit integer times.

The access location of the object. Java is a reference object reference in the stack to manipulate the specific object, but how the reference is positioned to the specific object, there are two ways to navigate:

The first type is the handle. The reference in the stack point to the handle pool of allocated memory in the heap, where the handle pool contains the address information of the class metadata and the address information of the instance object, respectively, the specific object in the heap and the class metadata in the method area.

        

The other is a direct pointer. The direct pointer is reference to the object in the heap, but the type-type pointer in the object header in the object points to the class metadata in the method area.

    

Both methods have advantages, the first is mainly stable, because the migration of objects in the virtual machine is very common, the understanding of GC memory recycling should be aware that the replication algorithm divides the heap into two pieces of which are used to store objects, and when memory reclamation is required, the objects that are not reclaimed are copied to another piece of memory. However, the migration of an object has no effect on reference, and only the address of the instance data in the handle pool is changed. The second is the improvement of performance efficiency, due to the disappearance of the address pointer of the instance data in the heap, is the performance improvement of the object invocation, although one is not obvious, when the object in the heap accesses very frequently, the performance improvement is very obvious. The current use of more than the second kind, but also not absolute.

   2. Method area

    The method area is the same area as the heap, which stores the class metadata, static variables, constants, and instant compiler code. This area is generally referred to as permanent generation, mainly because it is rarely cleaned by FULL-GC. The run-time constant pool is also part of the method area, and the class file is part of a const pool, which includes literal and symbolic references (literals are usually constants that are converted to the final decoration at compile-times, symbolic references are direct references, and the parsing phase in the connection resolves the symbolic reference to a direct reference). The symbol references the direct reference after conversion and Chang together into the run-time-constant pool. There is a distinct feature of the run-time pool that is dynamic, constants in a constant pool are not necessarily generated only at compile time, and the runtime may also generate constants and put them into constant pools, such as The Intern () method in string.

  

This article is only for the learning process of notes collation, welcome to the text of the inappropriate comments. Reprint Please specify address: http://www.cnblogs.com/qven/p/8734534.html

Bibliography: In-depth understanding of Java Virtual machines: JVM advanced Features and best Practices (latest second edition)

    

    

  

Drill down into the Java memory area

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.