The comparison of "=null" and "GC" when Java cleans objects __java

Source: Internet
Author: User

Today's topic is to use =null or GC when cleaning objects, and we'll make comparisons below.


1. The reason for writing this article

Before I wrote an article is the object of the cleanup (click Jump), one of the netizens gave another method, so I deliberately use the visual VM to test it out.



2. Visual VM using instrumentation Tools

Download Address: Https://java.net/projects/visualvm/downloads/download/release138/visualvm_138.zip

Use the reference article:

Performance analysis and tuning using VISUALVM


3. Test Cases

The following is the use of GC to clean up:

Package com.ray.ch05;

public class Test {public
	static void Main (string[] args) throws Interruptedexception {
		thread.sleep (15000); 
  SYSTEM.OUT.PRINTLN ("Begin Time:" + Dateutil.getnow ());
		for (long i = 0; i < 1999999999; i++) {
			test test = new test ();
			Test = null;
			System.GC ();
		}
		System.out.println ("End time:" + Dateutil.getnow ());
	}
}




From the top two figures can be seen, when using GC, test uses less memory to form, but because of the frequent GC, the Cup occupies very high, unstable, and GC for a very long time, the data above is not complete all GC data, but also can explain some problems.


Let's use =null to clean up the object:

Code

Package com.ray.ch05;

public class Test {public
	static void Main (string[] args) throws Interruptedexception {
		thread.sleep (15000); 
  SYSTEM.OUT.PRINTLN ("Begin Time:" + Dateutil.getnow ());
		for (long i = 0; i < 1999999999; i++) {
			test test = new test ();
			Test = null;
			System.GC ();
		}
		System.out.println ("End time:" + Dateutil.getnow ());
	}
}





As you can see from the image above, when you use =null to empty objects, cup occupancy is also high, but more stable, the use of memory than the GC is about 25% higher, generate a large number of instances, but there is a more obvious advantage is that the execution time is short, according to the author observed that when the instance of test reached a certain number of times, It would suddenly drop, and the individual speculated that the GC had been executed at that time, only to speculate that there was no actual basis.


Summary: In fact, the way to use =null is not really empty objects, objects are only recycled, you need to wait until a certain time to clean up, and the GC is directly clean, but, the use of =null faster, the use of GC execution speed is slow.

Above is only the author rough observation, has the question please point out, thanks.

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.