The JVM learns the memory allocation one

Source: Internet
Author: User

Transfer from: http://blog.csdn.net/mazhimazh/article/details/16879055, Dochebeau Master Share

we know that the basic composition of the computer is: the operator, controller, memory, input and output devices, the JVM also has this set of elements, the operator is of course, the hardware CPU is also processed, just in order to adapt to "once compiled, run anywhere" situation, need to do a translation action, So I used the JVM instruction set, which is a bit like the assembly command set, each assembly command set for a series of CPUs, such as the 8086 series of the assembly can also be used in 8088, but cannot run on 8051, and the JVM command set can be run everywhere, because the JVM did the translation, Translate into different machine languages depending on the CPU. the basic structure of its virtual machine is as shown.

What we need to focus on is stack and heap, which are stacks and stacks, and they are very extensive in practical applications.

1, Stack

The Java stack is made up of many stack frames (frame), and a stack frame contains the call state of a Java method. When a Java method is called out of the box, the JVM presses a new stack frame into the thread's Java stack, and when the method returns, the stack frame is ejected from the Java stack and discarded.
The stack frame consists of three parts: The local variable area, the operand stack, and the frame data area.

    • The local variable area is organized into an array with a single element length of one JVM word (note not a machine word), which is used to hold local variables (locally Variables), including input parameters and output parameters, as well as variables within the method;
    • The operand stack is also organized into an array of unit length as a single JVM word, but the array can only be accessed through standard stack operations (push and Pop), which is the only source of operands that are uniquely identified by the ALU (logical) CPU. When a method returns a value, it is usually returned by popping a data from the stack.
    • The frame data area holds some data to support constant pool parsing, normal method return, and exception dispatch mechanisms.

If there is a local variable area a,b,c, to calculate the c=a+b, use 4 instructions:

    1. Read the value of the local variable area [0] and press into the operand stack;
    2. Read the value of the local variable area [1] and press into the operand stack;
    3. pops up the top value of the operand stack, then pops up the top value of the operand stack, adds, and presses the result into the operand stack;
    4. POPs the top value of the operand stack and writes to the local variable area [2].
1  Public classTest {2      Public Static voidMain (String args[]) {3         intA =2; 4         intb =3; 5         intc = A +b; 6     }  7}

The code for its class file is shown below.

1 0iconst_22 1Istore_1 [A]3 2Iconst_34 3istore_2 [b]5 4Iload_1 [A]6 5Iload_2 [b]7 6Iadd8 7Istore_3 [C]9 8  return  

As you can see, the values of 2 and 2 are stored in the local variable area, then popped into the operand stack, and finally the add operation, the results are filled in the local variable area, the data in the stack of the operand.

2. Heap  

There is only one heap class stored in a JVM instance, and the size of the heap memory can be adjusted. After the class loader reads the class file, it is necessary to put the class, method, and constant variables into the heap memory to facilitate execution by the executor. In general, the memory allocation of objects is done on the heap.

In the course of running a Java program, a large number of objects are generated, some of which are related to business information, such as session objects, threads, and socket connections in HTTP requests, which are directly linked to the business and therefore have a longer life cycle. There are also objects, mainly the temporary variables generated during the run of the program, which are short-life cycles, such as: string objects, because of their invariant class characteristics, the system produces a large number of these objects, and some objects can be recycled even once.

as can be seen, the life cycle of different objects is not the same . Therefore, different life cycle objects can be collected in different ways to improve the efficiency of recycling. The total of the virtual machines is divided into three generations: young Generation, Old Generation, and persistent (Permanent Generation). The persistent generations primarily store Java-class information, which is not much related to the Java objects that garbage collection collects. The division of the younger generation and the old generation is more significant for garbage collection. Such as.

New Generation (young Generation):

When an object is created, the allocation of memory occurs first in the newborn area (large objects can be created directly in the new zone), and most of the objects are not used soon after they are created, so they quickly become unreachable, and are then cleared by the GC mechanism of the newborn area.

The memory allocations on the new zone are as follows: The new zone can be divided into 3 regions, the Eden area (the area that represents the first allocation of memory), and two survival zones (Survivor 0, Survivor 1). The main strategy algorithm for the memory allocation and recovery process is the stop-copy (stop-and-copy) cleanup method, but this does not mean that the stop-copy cleanup method is efficient.

  1. Most of the objects just created will be allocated to the Eden area, where most of the objects will soon perish. The Eden Zone is a contiguous memory space, so allocating memory on it is extremely fast;
  2. When Eden is full, the minor GC is executed, the extinct objects are cleared, and the remaining objects are copied to a surviving area Survivor0 (at this time, Survivor1 is blank, two survivor always have a blank), and thereafter, every time the Eden area is full, The minor GC is executed once, and the remaining objects are added to the Survivor0;
  3. Once the two survival zones have been switched several times (the Hotspot virtual machine defaults 15 times, with-xx:maxtenuringthreshold control, greater than this value entering the old age), the surviving objects (in fact only a small part, for example, our own defined objects) will be copied to the old age.

In the Eden area, the hotspot virtual machine uses two techniques to speed up memory allocation. respectively, Bump-the-pointer and Tlab (thread-local Allocation buffers), the two techniques are: Because the Eden Zone is continuous, Therefore, the core of Bump-the-pointer technology is to track the last object created, when the object is created, only need to check if there is enough memory behind the last object, which greatly accelerates the memory allocation speed, and for Tlab technology is for multithreading, Divide the Eden area into segments, each of which uses a separate section to avoid interaction. Tlab combined with Bump-the-pointer technology will ensure that each thread uses a segment of the Eden Zone and allocates memory quickly.

Older generation (young Generation):

If the object survives long enough in the young generation and is not cleaned up (that is, survived several times after the youth GC), it will be replicated to older generations, where older generations are generally larger than the younger generation and can store more objects, and fewer GC occurrences occur in older generations than in younger generations. When the old generation is out of memory, the major GC will be executed, also called the full GC.

If the object is larger (such as a long string or a large array), and young is not in space, the large object is allocated directly to the old age.

There may be cases where older generations of objects refer to a new generation of objects, and if you need to perform a young GC, you may need to query the entire old age to determine whether you can clean up the collection, which is obviously inefficient. The solution is to maintain a block of byte-"card table" in the old generation, where all the records of older objects referencing the Cenozoic objects are recorded here. Young GC, as long as check here can, no longer to check all the old age, so performance greatly improved.

The old age stores more objects than the younger generation, and there are large objects, in the old age of memory cleanup, if you use the stop-copy algorithm, it is quite inefficient. In general, the algorithm used in the old age is the marker-collation algorithm.

In two stages, the first stage marks all referenced objects starting from the root node, the second stage traverses the entire heap, clears the unlabeled objects and "compresses" the surviving objects into one of the heaps, in order. This algorithm does not produce fragmentation.

when the minor GC occurs, the virtual opportunity checks whether the size of each promotion into the old age is greater than the remaining space size of the old age, and if it is greater, it triggers the full GC directly, otherwise, the-xx:+ is set. Handlepromotionfailure (allow warranty failure), if allowed, only minor GC will be tolerated, memory allocation failure is tolerable, and if not allowed, full GC is still performed (this represents if the setting-xx:+handle Promotionfailure, triggering the MINORGC will trigger the full GC at the same time, even if there is a lot of memory in the old age, so it's best not to do so.

Durable Generation (Permanent Generation) :

Used to store static files, now Java classes, methods, and so on. The persistence generation has no significant impact on garbage collection, but some applications may dynamically generate or invoke some classes, such as Hibernate, at which point a large, persistent generation space is required to store the new class in these runs. The persistent generation size is set by-xx:maxpermsize=<n>.

There are two types of recovery for a permanent generation: constant pool constants, useless class information, constant recycling is simple, no references can be recycled. For recycling of useless classes, 3 points must be guaranteed:

    1. All instances of the class have been recycled
    2. The ClassLoader of the loaded class has been recycled
    3. Class object is not referenced (that is, where the class is not referenced by reflection)
recovery of a permanent generation is not required, and parameters can be used to set whether the class is recycled. Hotspot provides-XNOCLASSGC for controlled use-verbose,-xx:+traceclassloading,-xx:+traceclassunloading can view class loading and unloading information-verbose,-xx:+traceclassloading can be used in the product version of the hotspot;-xx:+traceclassunloading requires Fastdebug version of hotspot support3, Stack and heap1) The stack is the unit of the runtime, and the heap is the unit of storage.

Stack solves the problem of how the program executes, or how the data is handled, and what the heap solves is the problem of data storage, that is, where the data is placed and where it is placed.

It is easy to understand that a thread in Java has a line stacks corresponding to it, because different thread execution logic differs and therefore requires a separate line stacks. The heap is shared by all threads. The stack is a running unit, so the information stored in it is related to the current thread (or program). This includes local variables, program run states, method return values, and so on, while the heap is only responsible for storing object information.

2) Why should the heap and stack be differentiated? Is it possible to store data in the stack?

First, from the point of view of software design, the stack represents the processing logic, and the heap represents the data. This separation makes the processing logic clearer. The idea of divide and conquer. This idea of isolation and modularity is reflected in every aspect of software design.

Second, the separation of the heap from the stack allows the contents of the heap to be shared by multiple stacks (and can also be understood as multiple threads accessing the same object). The benefits of this sharing are many. On the one hand, this kind of sharing provides an effective way of data interaction (for example: Shared memory), on the other hand, the shared constants and caches in the heap can be accessed by all stacks, saving space.

Third, the stack because of the needs of the runtime, such as saving the context of the operation of the system, the address segment needs to be divided. Because the stack can only grow upward, it restricts the ability to store content on the stack. While the heap is different, the objects in the heap can grow dynamically as needed, so the stack and heap splits make the dynamic growth possible, and only one address in the heap is required to be recorded in the corresponding stack.

The object-oriented is the perfect combination of heap and stack. In fact, the object-oriented approach to the implementation of the previous structured program is not any different. However, the introduction of object-oriented, so that the way to think about the problem has changed, and closer to the natural way of thinking. When we take the object apart, you will find that the object's properties are actually data, stored in the heap, and the object's behavior (method) is running logic, placed in the stack. When we write the object, we write the data structure, and also write the logic of the processing. I have to admit, object-oriented design is really beautiful.

3) What is stored in the heap? What is stored in the stack?

The object is stored in the heap. The stack is a reference to the base data type and the objects in the heap. The size of an object is not estimated, or can be dynamically changed, but in the stack, an object only corresponds to a 4btye reference (the benefit of stack separation).

Why not put the basic type in the heap? Because the space it occupies is typically 1~8 bytes-it takes less space, and because it is a basic type, there is no dynamic growth-fixed length, so storage in the stack is enough, if there is no meaning to put him in the heap (it will be wasted space, explained later). It can be said that the basic type and object references are stored in the stack, and are a number of bytes, so when the program runs, they are handled in a uniform manner. But the basic type, the object reference, and the object itself are different, because one is the data in the stack, one is the data in the heap. One of the most common problems is the problem with parameter passing in Java.

4) The parameters in Java pass the simultaneous value? Or a reference? (reference types include: class type, interface type, and array)

In the run stack, the basic type and the reference are treated the same, and are all passed values, so if it is a reference to a method call, it can also be understood as a value invocation of "reference value", that is, the processing of the reference is exactly the same as the base type. However, when the called method is entered, the value of the reference being passed is interpreted (or found) by the program to the object in the heap, which corresponds to the actual object. If you modify at this point, you are modifying the reference to the corresponding object instead of the reference itself, that is, the data in the heap is modified. So this is a change that can be maintained.

1  Public classTest2 {2      Public voidChange (StringBuffer A,intb,string c) {  3A.append ("xxxx"); 4b =2; 5c+="nnn"; 6System. out. println (c);//mmmmnnn7     }  8      Public Static voidMain (string[] args) {9StringBuffer a=NewStringBuffer ("yyyy"); TenString c="MMMM";  One         intb=3;  A         Newtest2 (). Change (A, b,c);  -System. out. println (a);//yyyyxxxx -System. out. println (b);//3 theSystem. out. println (c);//MMMM -     }   -    -}

Java, the size of the stack is set by-XSS, when the stack of data stored in a long time, you need to adjust this value appropriately, otherwise there will be Java.lang.StackOverflowError exception. The common occurrence of this exception is the inability to return recursion because the information saved in the stack is the record point returned by the method.

The JVM learns the memory allocation one

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.