Java Objects and garbage collection

Source: Internet
Author: User

1. The garbage collection mechanism is only responsible for reclaiming objects in the heap memory and does not reclaim any physical resources (such as database connections, network IO, etc.)

2. The program cannot accurately control the operation of the garbage collection, and the garbage collection will run at the appropriate time. When the object loses its reference permanently, the system reclaims its occupied memory at the appropriate time.

3. Before the garbage collection mechanism reclaims any objects, it always calls its finalize () method first.

The state of the object in memory:

Up to state: When an object is created, if more than one reference variable references him, the object is in the state of the program, and the program can invoke the object's instance variables and methods by referencing the variable.

Recoverable state: If a variable in the program does not reference it in any reference variable, it enters a recoverable state. In this state, the garbage collection mechanism prepares to reclaim the memory of the variable, and before it reclaims the object, the system invokes the Fnalize () method of the recoverable object used, and if there is a reference variable referencing the object, the object becomes up to the state again.

Unreachable state: When the contact of the object and the referenced variable is severed, and the system has called the Finalize () method of the object, the object has not been made reachable. At this point the object enters the unreachable state, and the resource used by the object is not started to be reclaimed until the object is in an unreachable state.

 Public classstatustranfer{ Public Static voidTest () {//When you perform this step, object 12345 is in a state of reachString A =NewString ("12345"); //When you perform this step, object 12345 is in a recoverable state//Object 123 is in the UP StateA =NewString ("123"); }     Public Static voidMain (string[] args) {test (); }}

Forced garbage Collection
The program can only control when an object is not referenced by a reference variable, but does not control when the system is garbage collected.

The program cannot precisely control the timing of Java garbage collection, but it can still force the system to be garbage collected ———— this force only notifies the system to be garbage collected, but whether the system is garbage collected is still uncertain. Most of the time, the program forces the system to be garbage collected with some effect.

Two ways to force garbage collection:

1. Call the System class's GC () static method: System.GC ()

2. Call the Runtime's GC () instance method: Runtime.getruntime (). GC ().

Java Objects and 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.