Java object and garbage collection, java object garbage collection

Source: Internet
Author: User

Java object and garbage collection, java object garbage collection

1. The garbage collection mechanism only recycles objects in the heap memory and does not recycle any physical resources (such as database connections and network I/O resources)

2. The program cannot accurately control the running of garbage collection, and the garbage collection will run at the right time. After an object is permanently referenced, the system recycles the memory occupied by the object at the appropriate time.

3. Before the garbage collection mechanism recycles any object, it always calls its finalize () method.

The status of the object in the memory:

Possible status: After an object is created, if more than one referenced variable references it, the object is reachable in the program, the program can call the instance variables and methods of the object by referencing variables.

Recoverable state: If a variable in the program does not reference any referenced variable, it enters the recoverable state. In this state, the garbage collection mechanism will prepare to recycle the memory corresponding to this variable. Before the object is recycled, the system will call the fnalize () method of the recoverable object, if a reference variable references this object, the object becomes reachable again.

Inaccessible state: When the connection between the object and the referenced variable is cut off and the finalize () method of the object has been called, the object is still not made reachable. At this time, the object enters the inaccessibility state. The system starts to recycle the resources occupied by the object only when the object is in the inaccessibility state.

Public class StatusTranfer {public static void test () {// when this step is executed, object 12345 is in the reachable state String a = new String ("12345"); // when this step is executed, object 12345 is in the recoverable state // object 123 is in the reachable State a = new String ("123");} public static void main (String [] args) {test ();}}

Forced garbage collection
The program can only control when an object is not referenced by reference variables, but cannot control when the system recycles garbage.

The program cannot accurately control the timing of java garbage collection, but it can still force the system to recycle the garbage-this kind of force only notifies the system to recycle the garbage, but the system is still not sure whether to recycle the garbage. Most of the time, the program forces the system to perform garbage collection.

Two Methods of forced garbage collection:

1. Call the static gc () method of the System class: System. gc ()

2. Call the gc () instance method of Runtime: Runtime. getRuntime (). gc ().

 

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.