Detailed understanding of Java Basics Recycling mechanism

Source: Internet
Author: User

Memory management has always been a need to be considered carefully in the past when I was engaged in C + + development. In C, we use the library function malloc () and free () Two library functions to allocate memory and release from the heap, while C + + uses operator new and delete to manage memory, for both of these ways, the latter is the operator and the former is the library function. The latter can be processed by the compiler and the former focuses on the internal data implementation, in object-oriented design, the latter can better combine the constructor of the custom object to implement memory allocation. However, after the contact with Java, we can easily manage the memory, the key is that the Java implementation of the automatic memory management mode, specifically what is it?


First, the memory model of the JVM to understand how the Java memory is being reclaimed, it is necessary to understand the internal memory model of the JVM: First look at the first picture:
This diagram basically describes the 5 parts involved in a running thread within a JVM. Because the JVM is a parallel computing model based on multithreading, each thread has its own independent memory run space, so there is no interference between threads and threads, and the data for virtual machine memory is shared with all threads on the other. Is it a little dizzy? Let's start by knowing each component individually:
(1), program counter
The program counter is used to point to the location or line number of the byte code of the current running thread A when the instruction is run. When the JVM performs multithreaded parallelism, it is essentially a rotation mechanism between multiple threads, because a CPU can execute only one instruction at a time, and in order to maximize the utilization of the CPU's running resources, a thread rotation is used, and when a thread executes idle, in order to prevent the thread from blocking subsequent thread requests, The thread will be removed and replaced by another thread, allowing the CPU to continue executing and thus improve efficiency. So when thread A gets pulled down, it's definitely going to re-switch to a continuation of the line A, how do you know where the front-end is going to go before it's removed? The program counter is to achieve this function, it saves the current execution of the byte code in the thread memory offset, so as to achieve good follow-up execution. As you can tell, each thread has its own independent program counter memory space to point to its own execution process, so this is private to the thread. If a thread executes a Java method, the program counter points to the instruction address (line number) of the virtual machine's bytecode, and if the local method is executed, the counter is empty.
(2), Java Virtual machine stack Java Virtual machine stack describes a thread in the Java method execution of the memory model: Each Java method is executed in the threads will create a stack frame, the stack frame to store local variables table, Operation Stack and other information. The most critical of these is the local variables table, which stores the basic data types (short, int, long, byte, char, double, float, Boolean), object reference types, and so on, that are known at compile time when the Java method is executed. The size of the local variable table is determined at compile time and does not modify the size of the table when it is executed. It is important to note that the table is also part of the thread-private.
(3), local method stack since the Java Virtual machine stack is serving the Java methods in the thread, the local method stack is serving the local method that executes in the thread, which stores the relevant basic data and other information that the local method uses is needed.
(4), the Java heap Java heap is the largest memory area that the JVM needs to maintain. It is the area of memory that is shared across all threads. In this region, the memory space allocation of all object instances and the spatial allocation of arrays are almost responsible. The Java heap's JVM is a battleground for memory recycling. The current mainstream memory recovery algorithm is divided into the main memory partition in the region. This area is primarily responsible for the allocation of individual object instances.
(5), the method area method area is mainly used to store the load of virtual machine load of class information, constant data, static variables, and compiler-compiled related methods of code. It is also a zone that is shared by all threads. It is like a static variable of a class that is loaded when the class is loaded and shared with the class. This static variable or static method is stored in the method area that is called by each thread when needed. There is a major component in the method area that is a constant pool. The pool stores a variety of literal and symbolic references generated by the compiler.
Having said the above 5 parts, let's take a look at the following diagram to make it easy to understand:
Second, the JVM memory partition after understanding the above JVM's memory model, the JVM memory can be effectively divided. The main division here is to partition the Java heap. Because this part of the area is the main battlefield for GC recycling. When memory is reclaimed, the Java heap is divided into 2 generations, and the method area (not the heap) is divided into a generation. This partitioning method is based on the garbage collection of the generational recovery mechanism. The two generations divided in the heap are young Generation and old Generation respectively. In young generation, it was divided into 3 areas, namely Eden (Eden). I'm also drunk with this name, from Suvivor and to Survivor. Eden is primarily used to open up memory space for newly created object instances, while the remaining two zones are the same size as the survivor and to survivor, and are primarily used to store the remaining objects after a garbage collection.
The old genneration is primarily used to store long-lived objects.
In the method area, which is the non-heap area, the permanent Generation is partitioned to store data such as class information, static fields, which are present as the class is loaded by the virtual machine. Basic does not participate in garbage collection. Third, the JVM garbage collection task and principle on the above, understand the JVM in the Java heap and non-heap memory partition, understand the sub-regions under each region and corresponding storage data. You can now describe how the JVM performs memory reclamation: the memory allocations for object instances are mainly in young Generation->eden, a contiguous area of free memory. Therefore, the allocation of memory in this area is very fast because there is no need to make a lookup of the available memory areas. In young Generation->from survivor and to Generation, the two surviving areas are always empty, so when garbage collection is done, the JVM uses algorithms to find an inactive object instance in Eden. The active object instance is then copied to one of the empty surviving areas, while the other survives to store the object instance stored at the last garbage collection, and the active object is also copied to the empty survivor, where the surviving area stores the active object instance. Next, leave the remaining two areas Eden and one of the survivor empty. As soon as Eden is empty, it can be opened again, and another surviving area is emptied to provide a storage place for active objects for the next garbage collection. In this process, there is a problem, as garbage collection continues to execute, more and more objects in the survival area, then what to do? The Java garbage collection mechanism will perform algorithmic statistics on the objects in the surviving area, copying objects with long surviving time to the old generation to release the storage space of the surviving area. These are the functions of several storage areas.
The memory recycling mechanism in young generation is described above, so how do you do memory recycling for objects in old generation and permannent generation? The recycling algorithm used is another: called "tag---clear-and-zip". Tag is worth the tag active object, clear is to reclaim long-lived objects, compression is to compress memory, is all the objects are saved at one end, leaving the other side of the blank memory area to facilitate the opening of new object space. This reduces memory fragmentation and improves memory utilization.



Iv. JVM Garbage Collection Methods The JVM provides 3 garbage collection methods, namely serial GC, parallel recycle GC, parallel GC. (1), serial GC refers to the entire scanning and copying process using a single-threaded way to apply to a single CPU, the new generation of space is small and the time required to suspend the application is not very high. The recovery method will be paused at the time of collection and the serial GC method used for generational recycling. (2), the parallel recovery of GC in the execution of a multi-threaded parallel way to execute. (3), Mark Recycling. Mainly for the recycling mechanism used in the old generation.
This is my information on the JVM garbage collection summary and my own understanding, I hope to be helpful to everyone.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Detailed understanding of Java Basics Recycling mechanism

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.