Java garbage Collection (GC) and the kind of reference

Source: Internet
Author: User

Garbage collection GC
public class MyObject {private String ID;
Public MyObject (String id) {this.id = ID; }
@Override public String toString () {return ' myobject{' + ' id= ' + id + ' \ ' + '} '    ;    } public void Finalize () {System.out.println ("Object Reclaim" +id); }    }
Reference type 1. Strong reference 2. Soft reference        3. Weak references
        4. Virtual ReferencesOutput results
Source
public class Gctest {    public static void main (string[] args) {      int length =5;       //--------------Create strong references------------        set<myobject> a=new hashset< Myobject> ();        for (int i=0;i<length;i++) {            MyObject ref=new MyObject ("Hard_" +i);          &NBSP;SYSTEM.OUT.PRINTLN ("Create Strong references:" +ref);           a.add (ref);       }        a=null;       //-------------creating soft references-------------        set<softreference<myobject>> Sa=new hashset<softreference<myobject>> ();        for (int i = 0; i < length; i++) {&NB Sp           softreference<myobject> ref=new softreference<myobject> (New MyObject (" Soft_ "+i);          &NBSP; System.out.println ("Create soft Reference:" +ref.get ());            Sa.add (ref);       }        System.GC ();       //-------------Create weak references-------------  &NBS P     set<weakreference<myobject>> wa=new hashset<weakreference<myobject>> ();         for (int i = 0; i < length; i++) {            WEAKREFERENCE&L T Myobject> ref=new weakreference<myobject> (New MyObject ("Weak_" +i));            SYSTEM.OUT.PRINTLN ("Create weak reference:" +ref.get ());            Wa.add (ref);       }        System.GC ();       //-------------Create a virtual reference-------------  &nbs P     referencequeue<myobject> rq=new referencequeue<myobject> ();        Set <PhantomReference<MyObject>> pa=new HashSet<PhantomReference<MyObject>> ();
for (int i = 0; i < length; i++) {phantomreference<myobject> ref=new phantomreference<myobject>            (New MyObject ("Phantom_" +i), RQ);            SYSTEM.OUT.PRINTLN ("Create Virtual Reference:" +ref.get ());        Pa.add (ref);    } System.GC (); }}




From for notes (Wiz)

Java garbage Collection (GC) and the kind of reference

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.