Java memory management and garbage collection summary

Source: Internet
Author: User

Overview

A very important difference between Java and C + + is the management of memory, the automatic memory management of Java and the garbage collection technology, which simplifies the programming process by eliminating the need for Java programmers to release the memory of discarded objects, and avoids the memory leaks caused by the programmer's omission. Memory management and garbage collection is a very important part of the JVM, in-depth understanding of Java's memory management and garbage collection mechanism is to avoid and fix Java-related exceptions (OutOfMemoryError, Stackoverflowerror), understand the Java object creation process, The premise of efficient use of memory to build high-performance Java applications. This article will introduce the Java Runtime Memory area, garbage collection, object creation process.

Java run-time memory areaAs shown in the Java Runtime Memory area 2-1, the memory area is logically divided into: program counter, Stack, local method stack, heap, method area. Where the program counter, the stack, the local method stack are thread-private, the heap and the method area are shared by all threads. The program counter is used to indicate the line number of the bytecode that the current thread executes, and the stack is used to describe the memory model that the Java method executes, and whenever a new method is entered, the JVM creates a stack frame (holding local variables, parameters, return addresses, operand stacks) in the stack; The local method stack is the memory model of the local method execution , the hotspot virtual machine will combine the stack and the local method stack. when referring to stacks, we need to involve two exceptions: Stackoverflowerror and OutOfMemoryError, the difference between the two exceptions: when a thread requests a stack depth that exceeds the stack depth allowed by the virtual machine, Throws a stackoverflowerror, and OutOfMemoryError is thrown when the virtual machine cannot allocate enough space for the thread expansion stack. example of setting the stack depth:-xss128k ==>> Sets the depth of the stack to 128KB


Figure 2-1
Let's take a look at another very important area: heap, the Java heap is the primary area for storing Java object instances, and by New,clone, the objects created by deserialization are stored in the heap, why should Java store objects in the heap instead of stacks? C + + because there is no garbage collection mechanism, so when a variable is defined, its memory is allocated in the stack, only through new explicitly created an object, the object will be allocated memory from the heap, and at this time need to explicitly release the object by the delete memory, otherwise it will cause memory leaks. In addition to basic type variables (Boolean, Byte, char, short, int, long, float, double) in Java, other types of variables are basically created by new, so their memory is allocated from the heap, and when objects are discarded, The garbage collector automatically reclaims this portion of memory. Because the heap is an area of memory shared by each thread, storing objects in the heap facilitates communication between threads (shared memory). As we saw in the previous description of the stack, the JVM creates a stack frame for each method, so if the object is stored in the stack, the parameters of the method call will need to be copied from the stack frame of the calling method to the stack frame of the called method, and if the object is stored in the heap, only the pointer or reference is copied ( Two methods will point to the same object). So we can think of Java as the reason to store objects in the heap, one is Java has a very good garbage collection mechanism, the other is to store objects in the heap to facilitate the sharing of data and communication between threads, the third is to reduce unnecessary copy of the object, improve the efficiency of the method call, but also save memory. because different Java object lifecycles may be different, the heap is divided into two different areas based on the different life cycles of the Java objects: the Cenozoic and the old, the life cycle of the new generation is short, the survival rate is low, the life cycle of the object is long and the survival rate is high. Based on the different survival rate, the garbage collection in these two regions also uses different algorithms, the new generation generally uses the replication algorithm, the old age generally uses the mark-delete or the marker-collation algorithm. The copy algorithm is to copy the surviving objects from one region to another, tag deletion and labeling is to mark the objects that need to be recycled, and then clear out, and the tag grooming algorithm also organizes the memory so that memory fragmentation can be avoided. The garbage collection method that divides the Java heap into two different eras and uses different recovery algorithms is called generational collection. The following section details the garbage collection mechanism and the common garbage collector. after introducing the Java heap, let's take a look at the method area, where the method area is used to hold the class information, the constant (final, static final), the static variable, and the immediate compiler-compiled code. The hotspot virtual machine calls this section a permanent generation because the hotspot virtual machine extends the generational collection to the method area, or we can say that the hotspot virtual machine implements the method area through the permanent generation, and also provides the parameter-xx:maxpermsize to limit the maximum memory of the method area. However, this is not a good choice, when loading the analogy more or constant pool is larger, it is easy to cause memory overflow. At present, the official hotspot team has gradually adopted the local memory implementation method area, JDK1.7 has moved the constant pool out of the permanent generation. Memory overflow is also caused when memory in the zone does not meet the requirements. Garbage Collectionbased on the previous description of the memory area of the runtime, we know that garbage collection is mainly concentrated in the heap and method areas, the method area can selectively implement garbage collection, the area of garbage collection is mainly focused on recycling obsolete constants and type unloading. We've already mentioned the replication algorithm and the tagging algorithm, so how do we know which objects are discarded and which ones can't be recycled? For languages with garbage collection, two algorithms are generally used to determine discarded objects: the reference counting method (Python) and the Accessibility Analysis algorithm (also known as the root search algorithm, C#,lisp), Java uses the Accessibility analysis algorithm. The reference counting method determines whether an object is discarded by tracking its reference counter, and when a new reference points to the object, the reference count is incremented by 1, and when a reference does not point to the object, the reference count is reduced by 1 and the object is discarded when the reference count is 0 o'clock. When the algorithm encounters two object circular references in the heap (that is, there is a field in object A that points to object B, and Object B has a field pointing to a), a memory leak is caused, that is, the memory is never recycled, because the reference count for both objects is never 0. The problem with this algorithm is that the source of the reference ref to the object is not distinguished, and if ref is in the stack or the method area, the object is not obsolete, but if ref is in the heap, then we can continue to judge the memory area where the reference to the object where ref REF2 resides. This way, if you can eventually reach the stack, the local method stack, or the method area, it means that the object in the reference chain is not obsolete, otherwise it is obsolete, which is the accessibility analysis algorithm. In addition to using the Accessibility analysis algorithm to determine which objects need to be recycled, Java provides a different reference level for more flexible garbage collection. Java provides a total of four reference levels: Strong references, soft references (softreference), weak references (weakreference), virtual references (phantomreference). A strong reference is our general reference, a soft reference to an object that will be recycled when a memory overflow is about to occur (which can be used to implement caching), the object that the weak reference points to is reclaimed at the next garbage collection, and the object that the virtual reference points to will only receive a system notification on garbage collection. The life cycle of an object is not affected by virtual references at all. Let's take a look at the implementation of the replication algorithm (copy) and the tag-collation (mark-compact) algorithm, in fact, these two algorithms are improved from the tag-purge (Mark-sweep) algorithm, the tag cleanup algorithm will encounter two problems: the first problem is the efficiency problem, When the survival of the object is very low, in fact, the survival of the object to find and organize into a region, the efficiency will be higher, this is the replication algorithm; the second problem is that when the survival rate is high, there is a memory fragmentation problem, so there is a tagging-grooming algorithm. The Java heap uses the new generation as the replication algorithm, the old age is the token-collation or the mark-sweep algorithm's generational collection mechanism.


Figure 2-2 Marker-Purge algorithm



Figure 2-3 Replication Algorithm



Figure 2-3 Marker-Collation algorithm
The replication algorithm divides the Cenozoic into Eden,from survivor,to Survivor three blocks, and each garbage collection Eden,from Survivor The surviving objects are copied to the to Survivor. Remember that the division of these three regions is only logical, and is not related to the physical partition, the default Eden and Survivor size ratio is 8:1, the proportion is divided so large is to improve memory utilization, in this ratio can be used in memory actually only 90%; see here, I think a lot of people might ask, if 10% Space enough to store the surviving objects? The JVM provides a mechanism known as the allocation Guarantee (Handle Promotion), which is guaranteed by the old age for the to survivor space, if the to survivor does not have enough space to store the surviving objects, these objects are stored directly in the old age, if the old age is not enough to store , the OutOfMemoryError exception is thrown. After analyzing the idea of garbage collection algorithm, we have a look at the common garbage collector, the new generation of garbage collector including serial,parnew,parallel scavenge, the old garbage collector including Serial Old,cms,parold. One of the differences between these garbage collectors is single-threaded or multi-threaded, where serial,serial old is single-threaded and the rest is multithreaded; the second difference is whether the garbage collection thread and the user thread can execute concurrently, and the CMS collector can be divided into initial tags, concurrent tokens, re-tagging, Processes such as concurrent recycling, where concurrent tokens and concurrent collections can be executed concurrently with the user thread, so it is also the only real concurrency collector in these garbage collectors; Parallel scavenge and parold for increased throughput, other collectors to reduce the pause time (Stop The world) for the purpose. creating objects and memory allocationsAs mentioned earlier, the way Java creates objects includes new, clone, deserialization, and within the virtual machine, these three kinds of objects are actually created the same way. First to find or load the class information, if it does not load properly, then throw classnotfoundexception, otherwise to the Java heap allocation of memory, allocating memory in accordance with the memory is structured (depending on the garbage collection algorithm, labeling and replication algorithm memory is regular, Tag clear memory is not structured in two ways: pointer collisions and idle lists. In the way the pointer collides, the memory is divided into two parts by the pointer ptr, the part before PTR is used, the part after PTR is idle, and the pointer ptr = ptr + size when the object requires a size of memory. The idle list is a chain of idle areas through the linked list, and the object requires memory to traverse the linked list until it encounters an element with sufficient space. After allocating memory, the allocated memory is initialized to 0, each object has an object header, which holds the lock associated with the object, the object's hash code, the object's GC generational age, and a reference to the type in the method area. To this end, an object has been created successfully for the virtual machine, but from the Java program, this is just the beginning, followed by the <init> method to initialize all the fields. for different objects, the area of memory allocated is different. In general, prioritize allocating memory in Eden Space, and for large objects, allocate memory for older generations (size is greater than prenuresizethreshold), and objects will be moved to the old age when the object is older than Maxtenuringthreshold If the sum of all objects of the same age in the survivor space is greater than half the size of survivor space, objects older than or equal to that age enter the old age directly.

Java memory management and garbage collection summary

Related Article

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.