In-depth understanding of ClassLoader (v)-Class uninstallation

Source: Internet
Author: User

Original address: Http://yhjhappy234.blog.163.com/blog/static/316328322011101413730764/?suggestedreading&wumii

We know that when a class is loaded, connected, and initialized, his life cycle begins, and when the class object is no longer referenced, the life cycle of the class ends, and then the class is unloaded by the ClassLoader!

Let's look at the following code:

 Packagecom.yhj.jvm.classloader.uninstall;< xmlnamespace prefix = "o" ns = "Urn:schemas-microsoft-com:office:office"/>Importcom.yhj.jvm.classloader.user_defined. Myclassloader;/*** @Described: Uninstall Test for class *@authoryhj Create at 2011-10-17 pm 10:15:52 * @FileNmae Com.yhj.jvm.classloader.uninstall.UninstallTest.java*/ Public classUninstalltest {/**     * @paramargs *@throwsClassNotFoundException *@throwsillegalaccessexception *@throwsinstantiationexception * @Author yhj Create at 2011-10-17 pm 10:15:40*/     Public Static voidMain (string[] args)throwsclassnotfoundexception, Instantiationexception, illegalaccessexception {myclassloader classLoader1=NewMyclassloader ("ClassLoader1"); Classloader1.setclasspath ("D:/jvmtest/server/"); Class<?> clazz = Classloader1.loadclass ("TestCase1"); @SuppressWarnings ("Unused") Object obj=clazz.newinstance (); System.out.println ("1:" +Clazz.hashcode ()); Obj=NULL; System.out.println ("2:" +Clazz.hashcode ()); ClassLoader1=NULL; System.out.println ("3:" +Clazz.hashcode ()); Clazz=NULL; System.out.println ("==========="); ClassLoader1=NewMyclassloader ("ClassLoader1"); Classloader1.setclasspath ("D:/jvmtest/server/"); Clazz= Classloader1.loadclass ("TestCase1"); System.out.println ("4:" +Clazz.hashcode ()); }}

The following code first loads the class Testcase1through the custom ClassLoader classloader1 , then obtains the corresponding obj object by reflection, and then sets the corresponding object to null one by one To make it easier to destroy objects, what does the result look like?

We see that the first three times the print object's

The hashcode is consistent, stating that it is an object, but the last time it was changed, the address of the object changed, that is, it was unloaded (because we know that the class object will only be loaded once during the whole class loading process. There is only one instance object, unless the class is unloaded, and then it is loaded once (loaded 2 times).

Let's take a look at these kinds of relationships, and we'll draw them into graphs, as follows

The stack space variable points to the heap control's data, which has been debated before (previous articles),Loader1Object can be passedFindclassmethod to get the correspondingTestCase1Class, so there is a pointer to theTestCase1OfClassinstance, andTestCase1can be done bygetClassLoadermethod to get the corresponding ClassLoader, so there is a pointer to theMyclassloaderThe object,TestCase1Instances ofobjPassGetClassmethod to get the correspondingTestCase1Instance. So we can see,MyclassloaderObject has2A reference pointer,TestCase1Yes3A reference pointer, whenCloder1ForNULL,ObjclassForNULL,objForNULL, the system is leftMyclassloaderAndTestCase1A reference to each other.

We know that this class can be recycled as long as the class reference is not there, but the myclassloader and TestCase1 are not seen in the method area. There is a reference to each of these two actually have no other reference, but from the program results we can see that the class has been unloaded, indicating that the JVM has also done the two-way reference unloading and recycling operations, but according to our previous theory, only if the reference counter is 0 when it is recycled, how can it be understood? This we will in the following article, in-depth understanding of the jvm- garbage collection mechanism to do a detailed introduction, please look forward to Oh!

In-depth understanding of ClassLoader (v)-Class uninstallation

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.