Summarize the Java garbage collector methods and principles

Source: Internet
Author: User

1. Garbage collection is only memory-related

In Java, after we have finished new objects, the garbage collector is responsible for reclaiming the memory resources occupied by useless objects. This differs from C + + in that, in C + +, a local object is permitted, and a finalize () destructor is used when retrieving objects. C + + objects are created on the stack, and Java objects are created in the heap, so after we create the object, the Java garbage collector in the heap, will automatically help us to recycle garbage, as to when to recycle garbage, we do not have the unknown.

2. Methods for garbage collection

(1) Finalize ()

This method is used to reclaim "special" memory, which is not new, so the garbage collector cannot be recycled. This occurs primarily when using the local method, which is a non-Java code that is used in Java, can invoke any code, but can only be called by C and C + +. So we use Finalize () to release the memory generated by the local method.

(2) System.GC ()

This method is not recommended, serious consumption of performance, unless a last resort, generally not.

3.Java Garbage collector Works

When the garbage collector is working, it reclaims space on one side and the objects in the heap are rearranged. By rearranging the objects in the heap, a high-speed, infinite space-allocated heap model is realized.

4. Simple garbage Collection technology: Reference counting

Reference count: Each object has a reference counter, and when a reference is connected to an object, the reference counter automatically adds 1, and when the reference leaves the scope or is set to NULL, the reference counter is reduced by 1, and the JVM reclaims the object when the object's reference counter is 0. This method needs to set the reference counter on each object, which is inefficient and does not work well when it is used to cycle references between objects.

The working mechanism of the 5.Java garbage collector.

The Java Virtual machine takes an adaptive garbage collection technique.

stop-copy : Pauses the program first (it is not in background recycle mode), then copies all the surviving objects from the current heap to the other heap, the objects that are not copied are garbage, and the objects that are copied into the new heap are arranged in a compact way.

tag-sweep : Iterate through all references to find more surviving objects. When a surviving object is not found, it is tagged to the object, and no object is cleaned in the process. Garbage is not cleaned until all the marks have been completed.

During the run of a Java virtual machine, if all objects are stable and the garbage collector becomes less efficient, it switches to "tag-sweep", agreeing that the Java Virtual Opportunity tracks the "mark-sweep" effect, and if there is a lot of fragmentation in the heap space, it switches to "stop-copy";

6.Java Lifting Speed Technology: JIT

Jit:just-in-time, timely editing technology. The technology can translate the program into a local machine code. In the JDK, some of the frequently used. class files are intelligently compiled into bytecode and loaded into memory, so Java execution speed is improved.

Summarize the Java garbage collector methods and principles

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.