JVM Learning (II): Garbage Collection

I just asked a senior job, what can we do to optimize for the JVM. Senior said, the performance of our system now does not need tuning, with the default configuration will be able to meet the current needs. I asked, Why do you look JVM-related books? Seniors smile, leisurely to the sentence, in order to interview.
Joking aside, but in fact really adorable new programmer does not need to be JVM tuning in practical work. On the one hand the default configuration of the Java virtual machine enough for us to use, on the other hand a powerful IDE lets us basically in the preparation of the code does not need to consider the issue of virtual machines. So put aside to deal with the interview, why should we learn JVM it?
In my opinion, in terms of learning from the breadth and depth should work to advance the application. Like research, theory than practice never to go away (think Einstein predicted gravitational waves in 1916 and 2016 was only detected). Not much to say here, increase the stock of knowledge is certainly good. Without further ado, we began to talk garbage collection.

Speaking on a Java programmer does not need a manual garbage collection (GC), this is because the Java Virtual Machine has helped us to automatically complete. So why Java virtual machine to know which objects need to recover, when you need to recover, how to reallocate recovered it?
For the program counter, the virtual machine stack, native method stacks such non-threaded shared memory, their memory with the same thread of the life cycle, we do not consider these recovered memory. Here garbage collection refers to the thread shared area (heap and method area) of memory recovery.

What memory need to recycle?

Reference counting

Reference counting is an ancient survival algorithm to identify the object. Reference counting method is to add a number of citations for each object counter, statistical reference object, if the reference number is zero, then the object is judged to death.
This judgment method is one of the biggest drawbacks is that this method does not solve the object of a circular dependency problem. For example objects A and B, both refer to each other. At this point the two objects do not reference count is 0, then the two objects will never be recovered, so it is easy to cause memory leaks.

Reachability analysis

Java virtual machine is commonly used in reachability analysis algorithm, which is the initial survival of a collection of objects GC Roots. Then starting all objects from the collection of references to this set of live objects added to the collection. Ultimately, the object is not added to the collection will be judged as dead objects. If you are interested in the determination algorithm, written before I can refer to an article if there is a loop How to determine a figure .

However, this method is not a perfect solution. In a multithreaded environment, if a live objects in the object collection, during operation which reference is deleted, we believe that this object should be recovered out. But reachability analysis has not been completed, this time there will be omissions. Omission not be any problems, because the next reachability analysis is completed can be recovered out of memory for the object. Another case is false, the object is not in a survival object in the collection, before being recovered was a reference to another object, and this time we believe that this object is survival. However, this time the Java virtual machine may be recovered out of this object, it will cause a very serious problem. Therefore, garbage collection at the right time is very important.
GC Roots refers to an outer reference point to the stack in the stack, generally have the following categories:

  1. The method of Java local variable stack frame
  2. Static variables already loaded class
  3. JNI handles
  4. Started Java thread has not stopped

    When garbage collection?

    Stop-The-World

    In order to avoid false positives omission of the above problems, the Java virtual machine, garbage collection is carried out at some point during this period of change in the reference relationship does not appear, this time also known as Stop-the-world. During Stop-the-world, it will stop the work of other non-threaded garbage collection until the completion of garbage collection. Therefore, garbage collection caused a pause time (GC Pause).

During the Java virtual machine running, the program is difficult to expect to update references, so Stop-the-world is not uniform intervals, but rather to achieve a secure point (Safepoint) detection mechanism. Security detection is to find a stable state Java virtual machine stack is not updated when all threads have reached a safe point, will allow Stop-the-world garbage collection thread.

Thread generally steady state: JNI execute native code, interpreted byte code, and machine code execution thread blocking time compiler generated.

Java Native methods not to call Java objects or call Java methods, so the Java Virtual Machine stack will not change, it is a secure point.
Interpreted bytecode, the bytecode with the bytecode between can be seen as a safe point.
Instant compiled machine code is on the machine, run this part of the uncontrolled virtual machine directly run. Thus generating machine code when you want to insert security detection, avoid long waits pause caused. HotSpot VM approach is a method of generating code and the outlet non-counting loop is recycled back to the security at the edge point detection.
Blocked thread thread management is still in the Java virtual machine, so it is safe point.

Garbage collection mode

Remove

The most simple and crude way is cleared (Sweep). That is not the target memory alive in the collection of full recovery, recorded in a list of free memory. When the new target when it needed to draw from memory to spare inside the list.
Sweep

Since Java heap objects must be contiguous memory, so this approach has two obvious disadvantages. One is the low allocation efficiency, each allocation must traverse the list of free memory when memory, find the free memory block in line size. A second disadvantage is likely to cause memory fragmentation, there is a case where the memory space is sufficient, but there is not enough contiguous memory allocation for the new object (refer to the operating system memory management, it is the same reason).

compression

Compression means for the survival of the object memory, the memory is moved to the position of the starting point. Every conceivable recovery when they are needed to move the memory, so much the performance overhead of this approach.
Compact

copy

Copy the memory is divided into two halves, each with two pointers from and to maintain. To allocate memory from only a pointer to the memory area. When garbage collection, copy live objects to point to the memory area, and exchange content from and to points. This approach solves the problem of memory fragmentation, the disadvantage is greatly reduced usage of heap space (with only half).
copy

to sum up

Benpian talk about the basic principles of garbage collection. First, what kind of object is judged to be recovered, the main reference counting and reachability analysis. Secondly, we talked about when garbage collection, mainly Stop-the-world and security detection. Finally, the way garbage collection, are clear, compression and replication. Which will have to clear memory fragmentation, compression mechanisms have a greater algorithm overhead replication mechanism can solve memory fragmentation, but the heap space usage is low.

Reference article

Time Geeks - Zheng Yu Di: thorough dismantling of the Java Virtual Machine

In-depth understanding of the Java Virtual Machine: JVM advanced features and best practices

Guess you like

Origin www.cnblogs.com/rever/p/11417595.html