What is the basic principle of the "Java Noodles question" 50 garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify a virtual machine for garbage collection?

Source: Internet
Author: User

1 . For GC, when a programmer creates an object, the GC starts to monitor the address, size, and usage of the object.

Typically, a GC uses a graph to record and manage all objects in the heap. In this way, you determine which objects are "accessible" and which objects are "unreachable." When the GC determines that some objects are unreachable, it is the responsibility of the GC to reclaim those memory spaces.

2, can. The programmer can manually execute System.GC () to notify the GC to run, but the Java language specification does not guarantee that the GC will execute.

Second, here we have a detailed understanding of how System.GC () works:

Memory allocations in Java are implemented with new objects, which is simple, and there are mechanisms that can "improve" memory recycling, the most conspicuous of which is the System.GC () function.

At first glance, the function seems to be garbage collected, but the facts are not so simple.
In fact, this GC () function only reminds the virtual machine: The programmer wants to do a garbage collection. However, it does not guarantee that garbage collection will be carried out, and when the exact time is dependent on the virtual machine, different virtual machines have different countermeasures.

So the next question is:what are the criteria for GC () recycling? In other words, what kind of objects can be recycled?
Simply put: There are no objects that are pointed to by any of the available variables. I invented the one here .... The meaning is to be able to find, what kind of is not reached?
For example:

A.V == C; /*   *watch out!   */  = D;

Take a look at this piece of code:
First line: Object A's variable v points to object B
Second line: The variable v of object B points to the object C
Line six: The variable v of object a points to the variable d.
At this time, although the variable C pointed to the object has C and B.V point to it, but they are not reached, why? Because the only one who can find them is A.V, but now a.v points to D, so they are unreachable.

The reason is also very intuitive: there is no variable to point to you, you still have the reason to live? If you survive, who can find you?

So, the habit of leaving the freed pointer null in C + + is kept in Java, because it's probably the only way you can free up memory.

The last mantra: do not use GC functions frequently.
My advice is to keep the code robust (remember to set the unused variable to null) and let the virtual machine manage the memory.

What is the basic principle of the "Java Noodles question" 50 garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify a virtual machine for garbage collection?

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.