In-depth understanding of Java Virtual Machine (2) ------ Garbage Collector and memory allocation policy, ------ garbage collection

Source: Internet
Author: User
Tags gc collect

In-depth understanding of Java Virtual Machine (2) ------ Garbage Collector and memory allocation policy, ------ garbage collection

GC is one of the biggest differences between java and C ++.

1. What does GC collect?

In the previous article, we talked about the memory distribution.

The program counter stack, virtual machine stack, and local method Stack are generated with the thread and die with the thread. The memory of these stacks can be understood as being determined during the compilation period.

When the method ends or the thread ends, the memory is recycled.

The memory required for multiple implementation classes of an interface may be different. The memory required for multiple branches of a method may be different. We only know the objects to be created when the program is running, memory size required.

This part of allocation and collection are dynamic, and GC focuses on this part of memory.

2. recycling standards:

Almost all object instances are stored in the java heap. If the object instance is no longer in use, the memory should be recycled. This is the time when GC is used.

 

2.1 reference counting method:

A reference count is provided for an object. When the count is 0, it can be understood that the object is no longer used and the memory can be released.

However, this method is difficult to solve the problem of mutual reference between objects.

For example:

Both the object objA and objB have the field instance.

ObjA. instance = objB;

ObjB. instance = objA;

They have no other references.

However, their reference count cannot be 0, so GC cannot be notified to recycle them.

2.2 Accessibility Analysis Algorithm:

The mainstream implementations of mainstream commercial programming languages are called accessibility analysis.

The basic idea of this algorithm is to use the GC Roots object as the starting point. When an object is not connected to the start point, it can be understood that the memory of this node can be released.

As you can see, object5, object6, and object7 objects will be recycled by GC.

 

2.3 reference

No matter which algorithm is used to implement GC, it is related to references.

After java1.2, references are classified into strong references, soft applications, weak references, and virtual references.

Strong reference is a common state in java, similar to a new object. Strong references will not be recycled by GC.

Soft reference: A soft reference is an object that is still useful but not necessary. Its survival time is that when the memory is insufficient, that is, it is about to be OOM, GC will recycle it.

Weak references are unnecessary objects. Objects pointed to by weak references can only survive until the next GC.

Virtual references are used to receive a system notification when GC occurs. Does not affect the referenced object.

2.4 determine how to recycle objects:

GC will mark the unattainable objects for the first time.

After this object executes the finalize method, or does not overwrite the finalize method, this is the second mark. At this time, GC will be cleaned up by this memory segment.

When the object executes the finalize method, JVM will put it in an F-queue, which is the last chance for this object instance to save itself.

It only needs to assign this to a variable in finalize.

Any object finalize will be executed only once.

 

3. Garbage collection Algorithm

 

3.1 clearly marked algorithms:

Mark the memory to be recycled first. After the mark is complete, the memory will be recycled in a unified manner.

This is the most basic algorithm, and other algorithms are based on this.

3.2 replication algorithm

The memory is divided into two equal parts, each using only one piece, when one piece is used up, the surviving part is copied to another space, and then

Clean the used block at a time.

In this way, you do not need to consider memory fragments. The cost of this algorithm is half the memory space.

3.3 mark-Sorting Algorithm

Move all surviving objects to one end, and then directly clear the memory outside the end boundary.

3.4 generational Algorithms

Objects are divided into the new generation and the old generation based on their survival cycles.

In the new generation, many objects are recycled each time. You can use a replication algorithm, which only requires a small amount of surviving object replication costs.

In the old age, the survival time is relatively long, and you must mark clearly or mark the sorting method.

3.4.1 enumerate root nodes

When you select to process the GC Roots method, the GC Root context may contain hundreds of megabytes. Therefore, consistency should be ensured when determining GC Roots.

All java execution threads should be stopped. Therefore, GC consumes CPU at the cost!

 

4. Memory Allocation Policy

4.1 objects are preferentially allocated in Eden

In most cases, objects are allocated memory in the new generation of Eden. When the memory in the Eden area is insufficient, GC occurs on the Java Virtual Machine,

Release unwanted objects.

4.2 large objects are directly in the old age

A large object is an object that requires a large amount of continuous space, such as a long string or array.

A worse thing than a large object is to encounter a group of very useless large objects.

4.3 long-lived objects will enter the Old Age

Objects start to be placed in the new generation eden area. If a GC is used, enter the primary vor space and set the age of the object to 1.

After each GC, 1 is added to the age.

When the age reaches a threshold, it enters the old age.

 

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.