Tuning Java Virtual Machine FAQ summary Finishing

Source: Internet
Author: User
Tags tidy

Data Type
in a Java virtual machine, a data type can be divided into two categories: the base type and the reference type. A variable of the underlying type holds the original value, that is, the value he represents is the value itself, whereas a variable of the reference type holds the reference value. A reference value represents a reference to an object, not the object itself, where the object itself resides at the address represented by the reference value. basic types include: Byte,short,int,long,char,float,double,

Boolean,returnaddress reference types include: Class types, interface types, and arrays.

Heap and Stack
Heaps and stacks are key to the program's operation, and it's important to make their relationship clear. 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.

Why do you want to differentiate the heap from the stack? 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.

In Java, the main function is the starting point of the stack and the starting point of the program.

There is always a starting point for the program to run. Like the C language, main in Java is the starting point. No matter what Java program, find main to find the program execution of the portal.

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 it takes up 1 to 8 bytes of space, it requires less space. And because it is the basic type, there is no dynamic growth-fixed length, so storage in the stack is enough, if there is no point in the heap. 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.

What about parameters in Java passing simultaneous values? Or a reference?

in the stack, so when a parameter is passed, there is only a problem passing the base type and object reference. The object itself is not passed directly. Explicitly after the two points above. When Java passes a parameter in a method call, it is called by a value because there is no pointer. As a result, many of the books say that Java is a call-to-value, no problem. But how did the illusion of reference be caused? in the run stack, the basic type and reference processing is the same, is the value of the pass, so, if it is a reference to the method call, but also can be understood as a "reference value" of the call, that is, the reference processing is exactly the same as the basic type. However, when the called method is entered, the value of the reference being passed is interpreted 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 modification can be persisted. objects, in a sense, are made up of basic types. You can look at an object as a tree, the object's properties, if it is an object, is still a tree (that is, a non-leaf node), and the base type is the leaf node of the tree. When a program parameter is passed, the passed value itself cannot be modified, but if the value is a non-leaf node (that is, an object reference), you can modify all the content underneath the node.

Stacks and stacks are the most fundamental things that a program runs. A program can run without a heap, but not without a stack. The heap is a data storage service for the stack, and the heap is a shared memory. (heaps are serviced for stacks.) However, it is the idea of the separation of heaps and stacks that makes Java garbage collection possible. 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.

size of Java object

The size of the basic data type is fixed, and here is not much to say. For non-basic types of Java objects, its size is debatable. In Java, the size of an empty object object is 8byte, which is just the size of an object that holds no attributes in the heap. such as Object OB = new Object (); This completes the life of a Java object in the program, but it occupies a space of: 4byte+8byte. 4byte is the space required to save references in the Java stack as described in the above section. And that 8byte is information about the objects in the Java heap. Because all Java non-primitive types of objects require default inheritance of object objects, the size of any Java object must be greater than 8byte. With the size of the object, we can calculate the size of the other objects.

Class newobject{    int  count;       Boolean Flag;    Object ob;  

its size is: Empty object size (8byte) +int size (4byte) +boolean size (1byte) + NULL objects reference size (4byte) =17byte. However, because Java is divided by an integer multiples of 8 for object memory allocations, the nearest 8 integer times greater than 17byte is 24, so the size of this object is 24byte. It is important to note the size of the wrapper type for the base type. Because this type of packaging has become an object, they need to be treated as objects. The size of the wrapper type is at least 12byte (the space required to declare an empty object), and 12byte does not contain any valid information, and since the Java object size is an integer multiple of 8, the size of a basic type wrapper class is at least 16byte. This memory footprint is very scary, it is using the basic type of N times (n>2), some types of memory consumption is more exaggerated. Therefore, it is possible to use the packing class sparingly. after JDK5.0, the Java virtual opportunity is optimized for storage by adding automatic type swapping.

Why an empty object takes up 8 bytes

Java Design idea is that everything is the object of the world, the object is null is also an object, need to describe it, so to occupy memory, Java mechanism to empty object memory size is 8 bytes. Best Practice: Empty objects take up 8 bytes, and if you have data members, you press byte/boolean=1,char/short=2,int/float=4,long/double=8, object reference = 4 accumulate, and then align to a multiple of 8 bytes. If you inherit from another class, the parent class is counted as well.

Reference types---Object reference types into strong references, soft references, weak references, and virtual references.

Strong references: It is a reference that is generated by a virtual machine when we generally declare an object, and in a strongly referenced environment, the garbage collection needs to be strictly judged whether the current object is strongly referenced, and if it is strongly referenced, it will not be garbage collected. Soft References: Soft references are generally used as caches. The difference from strong references is that when a soft reference is garbage collected, the virtual opportunity determines whether to recycle the soft reference based on the remaining memory of the current system. If the remaining memory is strained, the virtual opportunity reclaims the space referenced by the soft reference, and if the remaining memory is relatively rich, it will not be recycled. In other words, when a virtual machine occurs outofmemory, there must be no soft reference present. Weak references: weak references are similar to soft references, and are used as caches. However, unlike soft references, a weak reference is bound to be reclaimed when it is garbage collected, so its life cycle only exists during a garbage collection cycle. Strong references Needless to say, our system is generally used as a strong reference. soft references and weak references are relatively rare. They are generally used as caches, and generally are cached when the memory size is limited. Because if the memory is large enough, you can use the strong reference directly as the cache, while the controllability is higher. As a result, they are commonly used in the desktop application system cache.

Basic garbage collection calculation---can divide garbage collection algorithm from different angle

according to the basic recycling strategy
reference count (Reference counting): an older collection algorithm. The principle is that this object has a reference, that is, adding a drink adds a count, and deleting a reference decreases one count. When garbage collection, only objects with a collection count of 0 are used. The most deadly of this algorithm is the inability to handle circular references.

mark-Clear (mark-sweep): mark the object that needs to be recycled, then scan it and have it tagged for recycling, resulting in two steps: Mark and clear. This algorithm is inefficient and will result in memory fragmentation when the cleanup is complete, so that if there is a large object that requires contiguous memory space, it needs to be defragmented.

replication: This algorithm delimits the memory space as two equal areas, using only one of the regions at a time. During garbage collection, iterate through the current usage area and copy the objects in use to another area. This algorithm only processes objects that are in use at a time, so the copy cost is small, and replication has been done in the past to make appropriate memory collation without "fragmentation" issues. The disadvantage is that it takes twice times the memory space.

mark-Organize (mark-compact): As in the first half of the tag-clear algorithm, just after you mark an object that does not need to be recycled, move the tagged object together so that the memory is contiguous so that the memory outside the tag boundary is cleared. This algorithm avoids the "mark-erase" fragmentation problem and avoids the space problem of the "copy" algorithm.

How to distinguish rubbish

The "reference counting" method mentioned above is judged by statistical control of the number of references to generate objects and delete objects. The garbage collector collects objects with a count of 0. However, this method does not resolve circular references. Therefore, in the garbage judgment algorithm that is implemented later, it starts from the root node of the program and traverses the whole object reference to find the surviving object. So in this way of implementation, where to start looking for which objects are being used by the current system. The above analysis of the difference between heap and stack, where the stack is really the execution of the program, so to get which objects are being used, you need to start from the Java stack . At the same time, a stack is corresponding to a thread, so if there are multiple threads, then all the stacks corresponding to those threads must be checked.

At the same time, in addition to the stack, there are system runtime registers, etc., is also stored program running data. Thus, the reference in the stack or register as a starting point, we can find the objects in the heap, and from these objects find a reference to other objects in the heap, the reference is gradually expanded to end with a null reference or a primitive type, so that a reference to the Java stack of the corresponding object is a root node Object Tree , which, if there are multiple references in the stack, will eventually form multiple object trees. Objects on these object trees are objects that are required for the current system to run and cannot be garbage collected. Other remaining objects can be treated as objects that cannot be referenced, and can be recycled as garbage. Therefore, the starting point for garbage collection is some root objects (Java stacks, static variables, registers) and the simplest Java stack is the main function that the Java program executes. This method of recycling is also the "mark-clear" recycling method mentioned above.

how to deal with fragmentation

Since the time to live for different Java objects is not necessarily, fragmented memory fragmentation occurs after the program has been running for a period of time without memory grooming. The most immediate problem with fragmentation is the inability to allocate large chunks of memory space, and the efficiency of the program running down. Therefore, in the above mentioned basic garbage collection algorithm, "copy" and "Mark-tidy" way, can solve the problem of fragmentation.

How to resolve concurrent object creation and object reclamation issues

Garbage Collection Threads recycle memory, while program running threads consume (or allocate) memory, one reclaims memory, and one allocates memory, and from this point of view, the two are contradictory. Therefore, in the existing garbage collection mode, before the garbage collection, it is generally necessary to suspend the entire application (that is, suspend the allocation of memory), and then garbage collection, after the recovery is completed before continuing to apply. This realization is the most direct and most effective way to solve the contradiction . However, there is a clear drawback of this approach, that is, when the heap space continues to increase, the time of garbage collection will continue to increase correspondingly, the corresponding application pause time will also increase correspondingly. Some applications that require a high amount of time, such as a maximum pause time of hundreds of milliseconds, are likely to exceed this limit when the heap space is larger than a few grams, in which case garbage collection becomes a bottleneck for the system to run. To resolve this contradiction, there is a concurrent garbage collection algorithm , which uses this algorithm to run the garbage collection thread concurrently with the program running thread. In this way, the problem of pausing is resolved, but because the object needs to be reclaimed at the same time as the newly generated object, the complexity of the algorithm will be greatly increased and the processing power of the system will be reduced correspondingly, and the "fragmentation" problem will be more difficult to solve.

what happens when garbage collection is triggered

There are two types of GC: Scavenge GC and full GC. Scavenge GC, in general, when a new object is generated and when the request space fails, the scavenge GC is triggered to GC, the non-surviving objects are cleared, and the surviving objects are moved to the survivor area. Then tidy up the two districts of survivor. This method of GC is carried out on the young generation of the Eden area and does not affect the old generation. Because most objects start in the Eden area, and the Eden area is not very large, GC in the Eden area is frequent. Thus, it is generally necessary to use fast and efficient algorithms, so that Eden can be free as soon as possible.

Full GC, which organizes the entire heap, including young, tenured, and perm. Full GC is slower than SCAVENGEGC because it needs to be recycled for the entire pair, so you should minimize the total GC number of times. In the process of tuning the JVM, a large part of the work is to adjust the FULLGC.

Tuning Java Virtual Machine FAQ summary Finishing

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.