The JVM area is generally divided into two categories, the heap and the non-heap areas. The heap is also divided into: Eden Space (Eden), Survivor Space (Survivor area), tenured Gen (old age-pension area).
Non-heap: code cache, Perm Gen (Permanent generation), JVM stack (Java Virtual machine stack), local method Statck (native methods Stack).
The hotspot virtual machine GC algorithm uses the Generational collection algorithm:
1, a person (object) out (new) will be in Eden Space(Eden) Carefree life, until the GC came to break their quiet life. The GC will ask each object what the case is, there is no money (reference to this object) Ah, because the GC wants to make money, money can blackmail it. Then the rich will enter Survivor space (Survivor area), and the poor will kill directly.
2, not enter Survivor Space(Survivor area) after the guarantee that the person is safe, but at least can live a period of time. The GC will be regularly (customizable) to extort these people, and every time the billionaire gives the money, the GC is satisfied and lets it enter the genured Gen (pension area). Wanyuanhu not live several times extortion there is no money, GC see no value, directly kill off.
3, into the genured Gen(pension area) people can basically ensure the safety of the person, but the billionaire will also splurge into the poor, as long as the money is gone, GC or kill off.
The purpose of the partition: the new district because the object produces more and most of it is toward the birth, so the direct use of marker-cleanup algorithm. And the old-life is very strong, the use of replication algorithm, for different situations using different algorithms.
The definition of classes and methods in Perm Gen in non-heap regions,
The JVM stack area has references to method parameters, local variables, etc., and the method execution order follows the stack's first-in and out-of-the-way.
Modern JVM memory Management method and GC implementation and main ideas
I would like to commemorate the death of the father of the C language, Dennis Ritchie. No matter how the world changes, no matter how the sun and Moon change, your glorious achievements are shining on the road of modern computer technology development.
When it comes to modern JVM memory management, it is important to mention a far-reaching thing, C language. C language Most people criticized, but also the C language is the most fascinating, is its memory management mechanism. In C, programmers have the freedom to control memory, their own decision in memory to write 0 or write 1. The so-called data type conversion, in the C language, but is the memory of a few copies and the location of the difference, that's it.
However, with the increasing scale of applications, whether it is the coupling of intertwined objects, or the huge amount of memory used, it makes developers claw. A few gigabytes of memory, the number of thousands of memory objects, is no longer a person or even 10 people can control the range. Moreover, hundred dense a sparse, as long as a little memory leaks, with the passage of time, it is possible to become an incomparable disaster. Oom and other problems, in the programmer's eyes, early is already a commonplace, who has not spilled memory, right.
It is true that there are masters who can control memory, but not everyone. So, how do you ensure that memory usage does not occur when large-scale team development? Code walk? Manual Check? Repeated tests? These can be done without first talking, even if feasible, the huge workload will allow all contracts to be extended to the next century. Then someone put forward an idea. Can not let a part of the master write a complete memory management module, plus a bunch of various libraries and standards, and finally constitute a huge runtime?
The idea was adopted by countless language teams. The first to achieve this is the Java team led by James Gosling. The goal of Java is write Once,run anywhere. It is estimated that they were in the coffee shop when the wrong time, it should be debug Anywhere, this is in line with the reality of the present, hehe. Pulled away, we looked back at memory management.
The JVM provides a number of class libraries that encapsulate a number of data types and common tool classes, which are used as their base libraries, such as Java.lang packages. For one of the simplest examples, let's say the simplest code. int i = 5;
In the C language, this sentence applies a few bytes of memory, and then put a 5 in, Java is the same. Only, C language to apply for the future to manage their own, and Java you do not have to worry about this thing, virtual opportunity to help you deal with. It will determine when it is needed and when it is not needed. From this, more complex business, such as connecting the database, read the file, we have to do is to call the class library, memory requests and releases are all taken over by the virtual machine, we do not have to move a finger.
We are cool, the virtual machine has a headache. So many objects, when should be destroyed, when should be kept, when to check these relationships? In the JVM, this thing has a module to do, which is the protagonist of our article, Gc,garbage Collection, garbage collection.
Suppose we are programmers implementing GC, so what are we going to do? First of all, responsible for allocating memory, responsible for controlling the holding count of objects, responsible for destroying memory objects, but also responsible for memory collation and so on. In the JVM specification Sun developed, detailed description of the GC section to do things, here do not repeat, want to see, please Google yourself.
The existing JVM, the mainstream, is hotspot and JRockit, the main research object is the two. In this article, we only study the hotspot, the so-called Sun JVM. At the present stage, Sun's GC mode mainly includes CMS and G1 two kinds. For results and practical applications, only the CMS is introduced here.
CMS, full name concurrent Mark Sweep, is the new GC algorithm introduced by JDK1.4 later version, which has been further improved in Jdk5 and JDK6, and its main fit scenario is the application of high demand for response time, and it is expected that this part of application can withstand the garbage collection thread and application thread sharing processor resources, and there are more long life-cycle objects in application 。 CMS is used to recycle tenured generation, which is the recycling of older generations, with the goal of minimizing application pauses, reducing the chance of full GC occurrences, and using garbage collection threads that are associated with the application thread to clear older generations.
The JVM will create a large number of objects in the process of running the program, most of which are short-period objects, a small part is a long-period object, and for short-period objects, frequent garbage collection is required to ensure that useless objects are released early, for long-period objects, Frequency garbage collection is not required to ensure unnecessary garbage scanning detection. To address this contradiction, the Sun JVM's memory management uses a generational strategy.
1) Young generation (Young Gen): the younger generation mainly stores newly created objects, the memory size is relatively small, garbage collection will be more frequent.
The young generation is divided into 1 Eden spaces and 2 suvivor space(named A and b).
When the object is created on the heap, it enters the young generation of Eden Space. When the garbage collector is garbage collected, it scans Eden Space and a suvivor space, and if the object is still alive, copy to B suvivor space, and if B suvivor space is full, copy to Old Gen. Also, when scanning suvivor space, if the object has been scanned several times and still survives, the JVM considers it to be a persisted object and moves it to old Gen. After the scan is complete, the JVM empties Eden Space and a suvivor space and swaps the roles of A and B (that is, the Eden Space and Bsuvivor space are scanned for the next garbage collection.) This is done primarily to reduce the generation of memory fragments.
We can see that young Gen garbage collection uses the method of copying the surviving objects to the empty Suvivor space to ensure that there is no memory fragmentation, and that space-time is used to accelerate the recovery of objects that are no longer held in memory as soon as possible.
2) Old generation (tenured Gen): Older generations primarily store objects that the JVM considers to be long-life cycles (after several years of young Gen's garbage collection still exist), the size of the memory is relatively large, and garbage collection is relatively less frequent (for example, several hours a time). Older generations mainly use compression to avoid memory fragmentation (moving the surviving objects to one side of the memory, i.e. memory grooming). Of course, some garbage collector (such as the CMS garbage collector) may not compress for efficiency reasons.
3) Persistent generation (Perm Gen): Persistent generations primarily store information that rarely changes, such as class definitions, bytecode, and static constants.
http://my.oschina.net/u/175660/blog/351702
The development process of modern JVM memory management method, GC implementation and related design overview (GO)