Research on garbage collection mechanism in Java

Source: Internet
Author: User
Tags garbage collection

A, who is doing garbage Collection?

A popular saying: in C + +, the system is doing garbage collection, and in Java, Java itself is doing.

In C + +, freeing the memory is handled manually, using the delete operator to free the allocated memory. This is the popular saying. Specifically, when the application thinks that an entity is not needed, it needs a delete to tell the system that it can reclaim the space. This request, to the coder, is a very troublesome, difficult to do. Any BBS, there are always a lot of topics about memory leaks in C + +.

Java uses a different, very convenient method: garbage Collection. The garbage collection mechanism is placed in the JVM. The JVM is fully responsible for garbage collection, applying space only when needed, and not having to worry about space recycling when discarding objects.

Second, when the object is discarded?

In C + +, when an object leaves its scope, the object is discarded.

Is that the lifetime of an object is no longer associated with its scope, but only with reference.

Java garbage collection mechanism generally contains nearly 10 kinds of algorithms. We don't need to be concerned about the majority of these algorithms. There is only one of the simplest: the reference counting method, which is related to coding.

An object that can have one or more reference variables pointing to it. When an object no longer has any reference variable pointing to it, the object is discarded by the application. Or, this object can be recycled by the garbage collection mechanism.

That is, when there is no reference to an object, it means that the application tells the JVM: I don't want this object, you can recycle it.

The JVM's garbage collection mechanism detects the heap space in real time. When an object is found to have a reference count of 0 o'clock, the object is included in the list to be reclaimed. However, it is not immediately destroyed.

Third, discard is recycled?

The object is deemed to be unnecessary, and the memory it occupies can be freed. The reclaimed memory can be used for subsequent redistribution.

However, it is not the object that is discarded and then recycled immediately. The JVM process does a lot of overhead with space recycling. If an application process discards one object at a time, it will reclaim its space immediately, making the system inefficient.

As mentioned earlier, there are multiple algorithms for the JVM's garbage collection mechanism. Other algorithms are used to determine when and how to recycle, except that the reference notation is used to determine whether the object has been discarded. The JVM's garbage collection mechanism has to be balanced between time and space.

Therefore, to improve system efficiency, the garbage collector usually runs only when two conditions are met: The object is recycled and the system needs to be recycled. Remember that garbage collection takes time, so the Java runtime system uses it only when it is needed. So you don't know exactly when the garbage collection happens.

Four, does not have the reference variable to point to the object useful?

Previously, the object that did not hang the reference variable was discarded, meaning that it was garbage in the heap space and could be reclaimed by the JVM at any time.

There is, however, an exception that is not an exception. For disposable objects (some books are called temporary objects), you can point to it without referencing a variable. One of the simplest and most common examples:

System.out.println ("I am java!");

is to create a string object and pass it directly to the println () method.

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.