Http://www.cnblogs.com/xrq730/p/4827590.html
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 (GB), from small to large TB, the adjacent units are 1024 times times,1024 is 2 of the 10-time side , 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. Kernel Space and user space
Connecting memory and registers is address bus, the width of the address bus affects the index range of the physical address, because the bus width determines how many bits the processor can get from the register or memory at one time, and also determines the maximum addressable address space of the processor. such as the 32-bit CPU system, the addressable range is 0x00000000~0xffffffff, that is, 232 = 4,294,967,296 memory locations, each memory location 1 bytes, that is, 32-bit CPU system can have 4GB of memory space. However, applications are not fully able to use these address spaces, because these address spaces are partitioned into kernel space and user space, and the program can use only the memory of the user space. Kernel space mainly refers to the program logic used by the operating system for program scheduling, virtual memory use, or link hardware resources. The purpose of distinguishing between kernel space and user space is mainly to be considered from the point of view of system stability. Windows 32 operating system default kernel space and user space is 1:1, that is, 2G kernel space, 2G memory space, 32-bit Linux system in the default scale is 1:3, that is, 1G kernel space, 3G memory space.
4. 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." First, the stack is not a concept, but two concepts, heap and stack are two different memory areas, simple to understand, the heap is used to store objects and stack is used to execute the program . 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 at the same time creates a stack frame , used to store the local variable table, operand stacks, dynamic links, method exits and other information, each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine into the stack into the stack process. The size of the stack is related to the implementation of the specific JVM, usually between 256k~756k.
(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.
3. Direct Memory
Think about adding this piece. Direct memory is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the Java VM Specification . However, this part of memory is also used frequently, and may also cause memory overflow problems. New NiO has been added to the JDK1.4, introducing a channel-to-buffer I/O approach that can be used to allocate out-of-heap memory directly using the native library, and then operate through a Directbytebuffer object stored in the Java heap as a reference to that memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap. Obviously, the allocation of native direct memory is not limited by the size of the Java heap, but since it is memory, it will certainly be limited by the size of the native total memory (including RAM, swap area) and processor addressing space.
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 2:java memory areas and objects