Java memory management and garbage collection summary

Source: Internet
Author: User

Overview

One important difference between Java and C + + is the management of memory. Java's own active memory management and garbage collection technology make Java program apes do not need to release the memory of discarded objects. This simplifies the programming process. At the same time, the problem of memory leaks caused by the omission of the program ape was avoided.

Memory management and garbage collection is a very important part of the JVM. An in-depth understanding of Java's memory management and garbage collection mechanisms is to avoid and fix Java-related exceptions (OutOfMemoryError, Stackoverflowerror), understand the Java object creation process, and make efficient use of memory. The premise of building high-performance Java applications. This article will introduce Java Execution memory area, garbage collection, object creation process.

Java execution-time memory areawhen Java executes memory area 2-1 as seen, the memory area is logically divided into: program counter, Stack, local method stack, heap, method area.

The program counters, stacks, and local method stacks 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 byte code that the current thread is running, and the stack is used to describe the memory model that the Java method runs. Whenever a new method is entered. The JVM creates a stack frame in the stack that holds local variables, and the number of references. return address, operand stack). The local method stack is the memory model that the local method runs. The hotspot virtual machine will combine the stack and the local method stack.

at the time of the mention to the stack. We need to involve two exceptions: Stackoverflowerror and OutOfMemoryError, the difference between the two exceptions is that when a thread requests a stack depth that exceeds the agreed stack depth of the virtual machine, it throws a Stackoverflowerror When the virtual machine cannot allocate enough space for the thread expansion stack, OutOfMemoryError is thrown. Demo sample setting Stack depth:-xss128k ==>> set Stack depth to 128KB


Figure 2-1
Let's take a look at a very important area: the heap. The Java heap is the primary area for storing instances of Java objects. Through new. Clone Deserialization creates objects that are stored in the heap, and why Java is storing objects in the heap. Rather than the stack? C + + because there is no garbage collection mechanism, when a variable is defined. The memory is allocated in the stack, and only when an object is explicitly created by new does the object allocate memory from the heap, and the memory that is consumed by the object is explicitly freed by the delete, otherwise it will cause a memory leak. In Java except for the basic type variables (Boolean, Byte, char, short, int, long, float, double), other types of variables are basically created through new, so their memory is allocated from the heap when the object is discarded. The garbage collector will voluntarily reclaim this portion of the memory itself.

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 described in the previous description stack, the JVM creates a stack frame for each method, so assuming that the object is stored in the stack, the method invocation's parameters will need to be copied from the stack frame of the calling method to the stack frame of the called method, assuming that the object is stored in the heap and only requires copying pointers or references (at this point). Two methods will point to the same object). So we can feel that Java is storing objects in the heap. One is that Java has an excellent garbage collection mechanism, and the other is that storing objects in the heap facilitates sharing of data and communication between threads, and the third is the ability to reduce unnecessary copying of objects. The efficiency of the method call is increased, and memory is saved at the same time.

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, and the life cycle of the object is long in the old age. High survival rate. Based on the different survival rates. The garbage collection in both regions also uses different algorithms. The new generation generally adopts the replication algorithm, which is used in the old age to mark-delete or mark-tidy algorithm.

The copy algorithm is to copy the surviving objects from one region to another, and tag delete and tag collation are the objects that need to be recycled. Then it clears out, and the tagging algorithm also organizes the memory. This prevents memory fragmentation.

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 describes the mechanisms for garbage collection and frequently uses garbage collectors.

after introducing the Java heap, let's take a look at the method area. A method area is a place for storing class information, constants (final, static final), static variables (statics), and code that is compiled by the immediate compiler. The hotspot virtual machine calls this part of the zone a permanent generation. Since the hotspot virtual machine extends the generational collection to the method area, or we can say that the hotspot virtual machine implements the method zone through a permanent generation, the same time provides the number of-xx:maxpermsize to limit the maximum memory of the method area. But in fact this is not a very good choice, when loading the analogy is more or the constant pool is larger. Very easy causes memory overflow. At the moment, the official hotspot team is gradually using 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 Collectionaccording to the previous description of the memory area, we know that garbage collection is mainly concentrated in the heap and the method area. The method area can selectively implement garbage collection. Garbage collection in this area is mainly focused on reclaiming 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 that have garbage collection capabilities. In general, two algorithms are 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 accessibility analysis algorithms. The reference counting method determines whether an object is discarded by tracking its reference counter, when a new reference points to the object. Reference count plus 1 when a reference is not pointing to the object. The reference count is reduced by 1 when the reference count is 0 o'clock. object is discarded. The algorithm encounters a circular reference to two objects in the heap (that is, object A has a field that points to object B). There is a field in object B that points to a), which leads to a memory leak, that is, this part of memory is never recycled. Because the reference count for both objects is never 0. The problem with this algorithm is that it does not differentiate the source of the reference ref that points to the object, and if ref is in the stack or method area, it means that the object is not obsolete, but if ref is in the heap. It is not certain that we can continue to infer the memory area where the reference to the object where ref REF2 resides. This way of backtracking, assuming that the stack is finally reached, the local method stack or the method area, then the reference chain of objects are not obsolete, otherwise are obsolete. This is the accessibility analysis algorithm.

In addition to the accessibility analysis algorithm, Java infers which objects need to be recycled. Different reference levels are also available to enable 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 usual way of quoting. A soft reference to an object is recycled when the system is about to have a memory overflow (can be used to implement caching), the object that the weak reference points to is reclaimed at the next garbage collection, the object that the virtual reference points to is simply receiving a system notification at garbage collection, and the life cycle of the object is completely unaffected by the virtual reference.

Let's take a look at the detailed implementation of the copy algorithm (copy) and the tag-collation (mark-compact) algorithm, in fact both of these algorithms are improved from the tag-purge (Mark-sweep) algorithm, and the markup cleanup algorithm encounters two problems: the first problem is the efficiency problem, When the survival rate of the object is very low, the fact that the surviving object is found and organized into a region, the efficiency will be higher, this is the replication algorithm; the second problem is when the survival rate is higher. There is a memory fragmentation issue. So there is the tag-collation algorithm.

The Java heap uses the new generation as a replication algorithm, and the old age is a generational collection mechanism for marker-collation or marker-purge algorithms.




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 block area. Every garbage collection Eden,from survivor is copied to survivor.

Remember that the divisions of these three regions are purely logical and not related to physical partitioning. The default Eden and Survivor size ratio is 8:1, the scale is divided so large is to improve memory utilization, at such a ratio of available memory is actually only 90%; see here, I think very many people may ask. What if 10% of the space is not 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. Let's take a look at the frequently used garbage collector, the new generation of garbage collector contains serial,parnew,parallel scavenge, the old garbage collector contains Serial. Cms,parold.

One difference between these garbage collectors is single-threaded or multithreaded. Among them, serial,serial old is single thread. The rest is multi-threaded. The second difference is whether the garbage collection thread and the user thread can run concurrently. The CMS collector can be divided into initial tags, concurrent tokens. Mark again. Processes such as concurrent collections, where concurrent tagging and concurrent recycling can run concurrently with the user thread. So it is also the only real concurrency collector in these garbage collectors; Parallel scavenge and parold to increase throughput. Other collectors to reduce the pause time (Stop the world) for the purpose.

creating objects and memory allocationsas mentioned earlier, Java creates objects in a way that includes new, clone, deserialization. Within the virtual machine, these three ways of creating objects are in fact the same. First Find or load class information. Assume that loading is not normal. ClassNotFoundException, otherwise allocating memory to the Java heap, allocating memory in a manner that depends on whether the memory is structured (depending on the garbage collection algorithm). The memory of the tag grooming and copying algorithms is structured, and the memory that is marked for cleanup is not structured in two ways: pointer collisions and spare lists. In the way the pointer collides, the memory is divided into two parts by the pointer ptr. The previous parts of PTR are used. The part after PTR is spare. When the object requires a size of memory. Pointer ptr = ptr + size. The spare list is connected by linking the spare area through the linked lists. The object needs memory to traverse the list until it encounters an element with sufficient space. Once the memory is allocated, the allocated memory is initialized to 0, and each object has an object header, which holds the object-related lock and the hash code of the object. The age of the GC generational of the object, and the related reference to the type in the method area. End For a virtual machine, an object has been created successfully, but from a Java program. This has just started, and the next run <init> method initializes all fields. for a different object. The area of the allocated memory is different.

Generally speaking. Prioritize allocating memory in Eden Space, for large objects. Prioritize allocating memory in the older age (size is greater than prenuresizethreshold), when the age of the object is greater than maxtenuringthreshold. Objects will also be moved to the old age. Assuming that 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 go directly into the old era.


Java memory management and garbage collection summary

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.