How GC (Gabage collection) works in Java

Source: Internet
Author: User

1.

"Reference count (reference counting)" is a simple but slow garbage collection technique. Each object contains a reference register, and when a reference is connected to an object, the reference count is added by 1. When the reference leaves the scope or is set to NULL, the reference count is reduced by 1. Although the overhead of managing reference counts is small, it requires continuous overhead throughout the program life cycle. The garbage collector iterates over the list of all objects, releasing the space it occupies when it finds that the reference count for an object is 0 o'clock. There is a flaw in this approach, and if there is a circular reference between objects, the "object should be recycled, but the reference count is not zero" scenario may occur. For the garbage collector, locating an object group that has an interactive reference is a significant amount of work required. Reference counts are often used to illustrate how garbage collection works, and it never seems to be applied to any Java virtual machine implementation.

2.

Java virtual machines will adopt an "adaptive" garbage collection technology. As for how to handle the found surviving objects, it depends on the different Java Virtual machine implementations. There is a practice called "Stop-Copy" (Stop-and-copy). This means that the program is paused (so it is not in background recycle mode), and then all surviving objects are copied from the current heap to the other heap, and all that is not replicated is garbage. When objects are copied to the new heap, they are next to each other, so the new heap is kept in a compact arrangement, and the new space can be easily and directly allocated as described above.

3.

There are many additional techniques in the Java Virtual machine to increase speed. In particular, the technology associated with loader operations, known as "instant" (Just-in-time,jit) compilation. This technique can translate all or part of a program into a local machine code (which would have been the work of a Java Virtual machine), and the speed of the program was increased. When you need to load a class (usually when you create the first object for that class), the compiler finds its. class file first, and then loads the bytecode of that class into memory. At this point, there are two options to choose from. One is to let the instant compiler compile all the code. However, this approach has two drawbacks: this loading action is scattered throughout the program life cycle, adding up to spend more time, and will increase the length of the executable code (bytecode than the real-time compiler after the expansion of the local machine code much smaller), which will lead to page scheduling, thereby reducing the program speed. Another approach is called Lazy compilation (Lazy uation), which means that the immediate compiler compiles code only when necessary. In this way, code that is never executed may not be compiled by JIT at all. Java hotspot Technology in the new JDK uses a similar approach, and the code performs some optimizations each time it is executed, so the more times it executes, the faster it will be.

Comments:

1. Garbage objects may not be garbage collected;

2. Garbage collection is not equal to "destructor " (differentiated from C + +)

How GC (Gabage collection) works in Java

Related Article

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.