Java Memory release

Source: Internet
Author: User

(Question one: What is a garbage collection mechanism?) )Garbage collection is a dynamic storage management technology, which automatically frees objects that are no longer referenced by the program, and implements the function of automatic resource recovery according to the specific garbage collection algorithm. When an object is no longer referenced, the memory reclaims the space it occupies so that space is used by later new objects to avoid a memory leak.
(question two: what are the characteristics of Java garbage collection?) )The Java language does not allow programmers to directly control the use of memory space. The allocation and recycling of memory space is done automatically by the JRE in the background, especially for garbage collection (Garbagecollection, also known as garbage collection), which can only be monitored and controlled by a super-thread provided by the running environment.
(question three: When will the garbage collector run?) )Garbage collection is typically done automatically when the CPU is idle or out of space, and programmers cannot precisely control the timing and order of garbage collection.
(question four: What objects meet the garbage collection criteria?) )When no gain thread can access an object, the object is eligible for garbage collection.
(question five: How does the garbage collector work?) )If the garbage collector finds that an object cannot be accessed by any live thread, he will consider the object to be eligible for deletion, adding it to the recycle queue, but not destroying the object immediately, and when destroying and freeing the memory is unpredictable. Garbage collection cannot be enforced, but Java provides methods (such as the System.GC () method) that allow you to request that the JVM perform garbage collection instead of requiring that the virtual opportunity do whatever it can to satisfy the request.However, there is no guarantee that the JVM will remove all unused objects from memory. (I've tried this method, and there are times when I can't release the memory.)
(question six: Can a Java program run out of memory?) )OK. The garbage collection system attempts to remove objects from memory when they are not being used. However, if you keep too many live objects, the system may run out of memory. The garbage collector does not guarantee enough memory to ensure that available memory is managed as efficiently as possible.
(question seven: How do I show objects that conform to the garbage collection criteria?) )
(1) null reference: When an object does not have a reference to him, he is eligible for garbage collection. In other words, if there is no reference to him, deleting the object's reference can achieve the purpose, so we can set the reference variable to NULL to meet the garbage collection criteria. (In fact, I have tried almost all of the following methods, and found that garbage collection is not executed quickly, it will have a slow process. )
Java code
StringBuffer sb = new StringBuffer ("Hello");
System.out.println (SB);
Sb=null;
(2) Re-assign a value to the reference variable: You can refer to another object by setting a reference variable to disassociate the reference variable from the reference to an object.

Java code
StringBuffer sb1 = new StringBuffer ("Hello");
StringBuffer SB2 = new StringBuffer ("Goodbye");
System.out.println (SB1);
sb1=sb2;//"Hello" at this time meets the recycling criteria
StringBuffer sb1 = new StringBuffer ("Hello");
StringBuffer SB2 = new StringBuffer ("Goodbye");
System.out.println (SB1);
sb1=sb2;//"Hello" at this time meets the recycling criteria

(3) object created within the method: The local variable created is only present within the duration of the method's action. Once the method returns, objects created within this method are eligible for garbage collection. One notable exception is the return object of the method. (if the calling class terminates, the private variables should also be freed)
Java code
public static void Main (string[] args) {
Date d = getDate ();
System.out.println ("D =" + D);
}
private static Date getDate () {
Date D2 = new Date ();
StringBuffer now = new StringBuffer (d2.tostring ());
System.out.println (now);
return D2;
}
public static void Main (string[] args) {
Date d = getDate ();
System.out.println ("D =" + D);
}
private static Date getDate () {
Date D2 = new Date ();
StringBuffer now = new StringBuffer (d2.tostring ());
System.out.println (now);
return D2;
}

(4) Quarantine reference: In this case, the object being reclaimed still has a reference, which is known as the isolated island. If there are two instances, they reference each other, and all other references to both objects are deleted, and no other thread can access either of the two objects. can also meet garbage collection conditions.
Java code
public class Island {
Island I;
public static void Main (string[] args) {
Island i2 = new Island ();
Island i3 = new Island ();
Island I4 = new Island ();
I2.i=i3;
I3.I=I4;
I4.i=i2;
I2=null;
I3=null;
I4=null;
}
}

public class Island {
Island I;
public static void Main (string[] args) {
Island i2 = new Island ();
Island i3 = new Island ();
Island I4 = new Island ();
I2.i=i3;
I3.I=I4;
I4.i=i2;
I2=null;
I3=null;
I4=null;
}
}

(Issue eight: Cleanup before garbage collection------Finalize () method) Java provides a mechanism that enables you to run some code before an object is just going to be garbage collected. This code is within a method named Finalize (), and all classes inherit this method from the object class. Because the garbage collector cannot be guaranteed to delete an object. Therefore, the code placed in finalize () cannot be guaranteed to run. It is therefore recommended that you do not rewrite finalize ();

Java Memory release

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.