Java Virtual machine learning 2:java memory areas and objects

Source: Internet
Author: User

The concept of several computers

For the future to write the article to consider, also to consolidate their knowledge and some basic concepts, here to clarify a few computer concepts.

1. Computer storage Unit

bits, bytes, kilobytes KB, mega M, gigabytes, TB, in turn, from small to large Between adjacent units are 1024 times times,1024 is 2 of the 10-time Square , that is

1Byte = 8bit,1k = 1024byte,1m = 1024k,1g = 1024m,1t = 1024G

2, Computer storage components

Register: Part of the CPU of the central processor, is the fastest read and write storage components in the computer, but the capacity is very small

Memory: A separate component that communicates with the CPU and is used to store the operational data in the CPU and the data exchanged with the external memory. Although today, the memory read and write speed is very fast, but because the register is on the CPU, so for the memory read and write speed and the register read and write speed There are several orders of magnitude gap. But there is no way, read/write I/O operations for memory is very difficult to eliminate, the number of registers is limited, it is not possible to complete all the operational tasks by register

3. Word length

CPU is one of the main technical indicators, refers to the CPU can be processed in parallel binary bits (bit). Typically, a CPU with a 8-bit cpu,32 bit of 8 bits of data is processed to handle binary data with a length of 32 bits at the same time. However, although the CPU is mostly 64-bit, it still runs at 32-bit length.

Objective

When it comes to Java memory areas, many people may first react as "stacks." The first stack is not a concept, but two concepts, the heap and the stack are two different areas of memory. Second, the heap memory and stack memory of this division is rough, this partitioning method can only show that most programmers are most concerned about the memory allocation with the most closely related to the memory area is these two pieces, the partition of Java memory area is actually far more complex than this. For Java programmers, with the help of the virtual machine automatic memory management mechanism, it is no longer necessary to pair the Delete/free code for each new operation, which is not prone to memory leaks and memory overflow problems. However, because Java gives memory control to a virtual machine, it is difficult to troubleshoot a memory leak and memory overflow, so a good Java programmer should be able to understand the memory areas of the virtual machine and the scenarios that can cause memory leaks and memory overflows.

Run-time data region

The Java Virtual machine (JVM) internally defines the area of memory that the program needs to use at run time, copying a picture from http://images.blogjava.net/blogjava_net/nkjava/jvmstructure.png

The reason for dividing so many areas is because these areas have their own uses, as well as the time to create and destroy them. Some regions exist as virtual machine processes start, and some regions depend on the user thread's start and end to destroy and build. The green part of the figure is the area of memory that is shared between all threads, while the white part is the data region that is unique to the thread when it runs, looking at these data areas from this classification perspective.

1. Thread-Exclusive Memory area

(1) Program COUNTER REGISTER, Procedure counter

This memory area is small, which is the line number indicator of the byte code executed by the current thread , and the bytecode interpreter selects the next byte-code instruction to execute by changing the value of the counter. Java method This counter has a value, if the execution is a native method, then this counter is empty.

(2) JAVA stack, virtual machine stack

The life cycle is the same as the thread . Each method executes and creates a stack frame that stores information such as local variable tables, operand stacks, dynamic links, method exits, and every method from the call to the completion of the process, It corresponds to a stack frame in the virtual machine into the stack to the stack process.

(3) Stack of NATIVE method stack

The same as the virtual machine stack, except that the method stack serves the native method used by the virtual machine. The virtual machine specification does not impose any enforcement on this area, so we are using a hotspot virtual machine that simply does not have this area, and it is together with the virtual machine stack.

2. Memory areas shared between threads

(1) heap, heap

In most applications, the heap is the largest piece of memory managed by a Java virtual machine, which is created when the virtual machine is started, and the only purpose of this memory is to hold object instances . As the garbage collector is now used by the basic collection algorithm, so the heap can also be subdivided into the new generation and the old age , and then the Eden area, from the Survivior area, to Survivor area, this will be discussed later.

(2) Method area

This area is used to store data such as class information, constants, static variables, and immediately compiled code of the virtual machine , which is described as a logical part of the heap, but actually it should be separate from the heap. From the perspective of the collection algorithm mentioned above, in the hotspot, the method area ≈ permanent generation. However, after JDK 7, the hotspot we use should not have a permanent generation of this concept, will use native memory to achieve the planning of the method area.

(3) Runtime CONSTANT pool, running constant-volume

The diagram above is not drawn because it is part of the method area. In addition to descriptive information about the class's version information, fields, methods, interfaces, and so on, there is also a constant pool that holds the various literal and symbolic references generated during compilation, which will be loaded into the run-time pool of the approach area when the class loads. In addition, direct references to translations are also stored in this area . Another feature of this area is the dynamic nature, Java does not require constants must be generated during the compilation, during the runtime can also put new content in this area, the String.intern () method is the application of this feature.

Object creation

Java is an object-oriented language, and Java programs are created with objects all the while running. At the language level, creating objects (cloning, deserialization) is a new keyword, but not at the virtual machine level. Take a look at the steps to create an object at the virtual machine level:

1, virtual opportunity to a new directive, first to check whether the parameters of this directive can be located in the constant pool of a class symbol reference, and check that the symbol reference represents the class has been loaded, parsed and initialized. If not, the initialization of the class must be performed first.

2. After the class load check passes, the virtual machine allocates memory for the new object. The amount of memory required for an object can be fully determined after the class is loaded, and allocating space for an object is nothing more than dividing a certain size of memory from the Java heap. This place will have two problems:

(1) If the memory is structured, the virtual machine will use the pointer collision method to allocate memory for the object. It means that all used memory on one side, free memory on the other side, the middle of a pointer as a demarcation point indicator, allocating memory is simply to move the pointer to the idle side of the size of the object is equal to the distance. If the garbage collector chooses serial, parnew this compression-based algorithm, the virtual machine uses this method of allocation.

(2) If the memory is not structured, the memory used and the unused memory are interleaved with each other, then the virtual machine will use an idle list method to allocate memory for the object. This means that the virtual machine maintains a list of which memory blocks are available, and then allocates a large enough space from the list to the object instance, and updates the contents on the list. If the garbage collector chooses a CMS based on the tag-purge algorithm, the virtual machine uses this allocation method.

Another issue guarantees thread safety in the new object in a timely manner. Because the virtual machine may be allocating memory to object A, the pointer has not yet been modified, and object B uses the original pointer to allocate memory. The virtual machine uses CAS with the failure retry method to ensure the atomicity of the update update operation and Tlab two ways to solve the problem.

3, the memory allocation ends, the virtual machine will allocate to the memory space is initialized to 0 values (excluding the object header). This step ensures that the instance fields of an object can be used directly in Java code without assigning an initial value, and the program can access the 0 values corresponding to the data types of those fields.

4. Make the necessary settings for the object, for example, the object is an instance of which class, how to find the metadata information of the class, the object's hash code, the object's GC generational age, and so on, which is stored in the object's object header.

5, the implementation of the <init> method, the object according to the programmer's intention to initialize, so that a truly usable object is fully produced.

This part of the content, if you have to download OpenJDK source code, you can refer to the Hotspot/src/share/vm/interpreter/bytecodeinterpreter.cpp file, starting from 1939 lines. The code for the 1939 line is case (_new): {...}, meaning that the modern code meets new in the keyword, the virtual machine does the thing. The actual virtual machine may not be the execution of this code, but this code to understand the new object when the virtual machine operation process is basically no problem.

Object Positioning method

The object is created to use the object, and the Java program needs to manipulate the concrete objects on the heap through the reference (reference) data on the stack. For example, we wrote a sentence

Object obj = new Object ()

In fact, there are two parts of the new Object (), some of which are class data (such as class objects representing classes), and part of the instance data

Because the reference is simply a reference to object new object () in the Java Virtual Machine specification, and does not specify how obj should locate and access the object's location in the heap, the way objects are accessed depends on the virtual machine. There are two main ways of doing this:

1, handle access. The Java heap divides a handle pool, and obj points to the handle address of the object, and the handle contains the address of the class data and the address of the instance data.

2, pointer access. The address of all instance data and class data in the object, and obj points to the object.

The hotspot virtual machine uses the latter, but the former object access method is also very common.

Java Virtual machine learning 2:java memory areas and objects

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.