Deep understanding of Java virtual machines

Source: Internet
Author: User

First, garbage collection

The Java Virtual machine's heap contains all the objects created by the running Java program. When an object is no longer referenced by a program, the heap space It uses can be recycled so that subsequent new objects are used. The garbage collector must be able to determine which objects are no longer referenced and can release the heap space they occupy. In the process of releasing an object that is no longer referenced, the garbage collector runs the finalization method (finalizer) of the object that will be disposed.

In addition to releasing objects that are no longer referenced, the garbage collector also handles heap fragmentation. In a virtual memory system, the additional paging (or swap) space required by the growing heap affects the performance of the running program.

Garbage Collection
Garbage collection algorithm to do two things
1) Detection of garbage objects
2) Reclaim the heap space used by the garbage object and return it to the program

  garbage detection is typically implemented by creating a collection of root objects and examining the accessibility that begins with those root objects. This object is accessible if the executing program has access to the root object and a reference path exists before an object. The root object is always accessible to the program. Starting with this root object, any object that can be touched is considered to be the active object. Objects that cannot be touched are considered garbage because they do not affect the execution of the program. The collection of root objects for a Java virtual machine varies by implementation, but it always contains the object references in the local variables and the stack frame's operand stacks (and the object references in the class variables). The source of another root object is an object reference in the constant pool of the loaded class, such as a string. Another source is a reference to an object that is passed into the local method without being disposed by the local method. Another source of potential root objects is the portion of the data area that is allocated from the garbage collector's heap when the Java virtual Runtime is running. The two basic ways to differentiate between active objects and garbage are reference counting and tracing. The reference count garbage collector distinguishes between active and junk objects by saving a count for each object in the heap. This count records the number of references to that object. The trace garbage collector actually tracks the reference graph starting from the root node. Objects encountered in a trace are labeled in some way, and when the trace is finished, the object that is not marked is determined to be untouchable and can be used as garbage collection.  There are two mechanisms for the exit of a Java program: one is that the program runs and exits naturally. The other is to throw an exception or exit with an error.  So when the main thread starts a child thread and the main thread exits in any of these ways.  Whether the child thread continues execution depends on if the thread is a user thread or a daemon thread. If the child thread is a user thread, the child thread continues execution. The main thread exits before the JVM exits. The JVM will determine the current process. If there is no user process (with or without a daemon), the JVM exits if the thread continues execution depending on whether the thread is a user thread or a daemon thread. If the thread is a user thread, the thread will continue to execute. If the thread is a daemon thread, the thread is terminated. 

Reference Count
Each object in the heap has a reference count. When an object is created and a reference to that object is assigned to a variable, the reference count of the object is set to 1. When any other variable is assigned a reference to the object, the count is added to 1. When a reference to an object exceeds the lifetime or is set to a new value, the object's reference count is reduced by 1. Any object with a reference number of 0 can be garbage collected. When an object is garbage collected, it references any object count value minus 1. In this approach, an object that is garbage collected may cause subsequent garbage collection of other objects.
The advantage is that garbage collection work can be done quickly. The disadvantage is that two or more object circular references cannot be detected.

Trace Collector
Tracks the object reference graph starting at the root node. Objects encountered during the tracing process are tagged in some way. When the trace is finished, the unmarked object knows it is unreachable and can be collected. (Mark and Clear)

Heap Fragmentation Collection
Tag and clear the collector uses two strategies: compress and copy to quickly move objects to reduce heap fragmentation.
The compression collector moves the active object to one end of the heap. A large contiguous idle area appears at the other end of the heap. References to all objects that are moved are also updated to point to the new location.
The copy collector moves all the active objects to a new location. In the process of copying, they are discharged next to each other, eliminating gaps in the old area.
The generic copy collector algorithm is "stop and copy." In this scenario, the heap is divided into two regions, which can only be used at any one time.

Collector collected by Generation
Disadvantages of the copy collector:
1) Most of the objects created by most programs have a very short life cycle.
2) Most programs create objects that have a very long life cycle.

One of the wasteful efficiency of copy collectors is that they copy the long-life objects back and forth each time, consuming a lot of time.

By substituting the collector to solve the problem under this efficiency by grouping objects by their life expectancy, and to collect more of those young objects that appear briefly.

The heap is divided into multiple sub-heaps, each of which is a generation. The youngest heap is the most frequent garbage collection. If a relatively young object continues to exist after several garbage collections, the object becomes a more life-long generation, moving to another sub-heap.

Large-scale garbage collection can take up a lot of resources and time, which can lead to pauses and inability to meet the requirements of real-time systems. Therefore, a progressive collection algorithm is used.
The train algorithm is designed to provide a progressive collection of time limits in the mature object space.

Carriages, trains and railway stations

The train algorithm divides the mature object space into a fixed-length block of memory that is executed individually in one block at a time. Each block belongs to a collection.
The block is called the train, the collection is called the railway, the mature object space is the railway station. The train was ordered and the block was attached to the rear of the train.

This approach represents the overall ordering of all blocks in the mature object space.

Compartment Collection

When the train algorithm executes, it collects the smallest digital carriages in the smallest digital train, or collects the entire smallest digital train. If the whole train is a garbage object, the whole train is collected. Otherwise, collect the smallest digital compartment.

When collecting the smallest digital compartment, if it is found that the interior of the compartment is referenced by other carriages, it will be transferred to the reference compartment, so that the whole compartment is collected.
When collecting the smallest number of trains, if found to have been referenced by other trains in the train, it will be transferred to the quoted train, so cycle, and finally collect the entire train.

memory sets and popular objects
To facilitate the collection process, the train algorithm uses a memory set. A memory set is a data structure that contains all the external references to a car or a train. An empty memory set indicates that objects in carriages or trains are no longer referenced by carriages or any variables outside the train, and can be collected by garbage.

Deep understanding of Java virtual machines

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.