java点滴(6)之java引用
生活随笔
收集整理的這篇文章主要介紹了
java点滴(6)之java引用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.強引用這是使用最普遍的引用。如果一個對象具有強引用,那就類似于必不可少的生活用品,垃圾回收器絕不會回收它。當內存空 間不足,Java虛擬機寧愿拋出OutOfMemoryError錯誤,使程序異常終止,也不會靠隨意回收具有強引用的對象來解決內存不足問題。2.軟引用(SoftReference)如果一個對象只具有軟引用,那就類似于可有可物的生活用品。如果內存空間足夠,垃圾回收器就不會回收它,如果內存空間不足了,就會回收這些對象的內存。只要垃圾回收器沒有回收它,該對象就可以被程序使用。軟引用可用來實現內存敏感的高速緩存。軟引用可以和一個引用隊列(ReferenceQueue)聯合使用,如果軟引用所引用的對象被垃圾回收,Java虛擬機就會把這個軟引用加入到與之關聯的引用隊列中。3.弱引用(WeakReference)如果一個對象只具有弱引用,那就類似于可有可物的生活用品。弱引用與軟引用的區別在于:只具有弱引用的對象擁有更短暫的生命周期。在垃圾回收器線程掃描它 所管轄的內存區域的過程中,一旦發現了只具有弱引用的對象,不管當前內存空間足夠與否,都會回收它的內存。不過,由于垃圾回收器是一個優先級很低的線程, 因此不一定會很快發現那些只具有弱引用的對象。弱引用可以和一個引用隊列(ReferenceQueue)聯合使用,如果弱引用所引用的對象被垃圾回收,Java虛擬機就會把這個弱引用加入到與之關聯的引用隊列中。4.虛引用(PhantomReference)"虛引用"顧名思義,就是形同虛設,與其他幾種引用都不同,虛引用并不會決定對象的生命周期。如果一個對象僅持有虛引用,那么它就和沒有任何引用一樣,在任何時候都可能被垃圾回收。虛引用主要用來跟蹤對象被垃圾回收的活動。虛引用與軟引用和弱引用的一個區別在于:虛引用必須和引用隊列(ReferenceQueue)聯合使用。當垃 圾回收器準備回收一個對象時,如果發現它還有虛引用,就會在回收對象的內存之前,把這個虛引用加入到與之關聯的引用隊列中。
?3?public?class?Student?{
?4?
?5?????private?int?id;
?6?????private?String?name;
?7?????private?int?age;
?8?????
?9?????public?Student(int?id,?String?name,?int?age)?{
10?????????super();
11?????????this.id?=?id;
12?????????this.name?=?name;
13?????????this.age?=?age;
14?????}
15?????
16?????@Override
17?????protected?void?finalize()?throws?Throwable?{
18?????????super.finalize();
19?????????System.out.println("finalize?:?"?+?toString());
20?????}
21?????
22?????@Override
23?????public?String?toString()?{
24?????????return?"?[Student?msg?:?id?=?"?+?id?+?"?name?=?"?+?name?+?"?age?=?"?+?age?+"]";
25?????}
26?????
27?????public?int?getId()?{
28?????????return?id;
29?????}
30?????public?void?setId(int?id)?{
31?????????this.id?=?id;
32?????}
33?????public?String?getName()?{
34?????????return?name;
35?????}
36?????public?void?setName(String?name)?{
37?????????this.name?=?name;
38?????}
39?????public?int?getAge()?{
40?????????return?age;
41?????}
42?????public?void?setAge(int?age)?{
43?????????this.age?=?age;
44?????}
45?????
46?}
?2?
?3?import?java.lang.ref.ReferenceQueue;
?4?import?java.lang.ref.WeakReference;
?5?
?6?import?com.test.reference.mode.Student;
?7?
?8?
?9?
10?public?class?ReferenceUsage?{
11?????public?static?void?main(String?args[])?{
12?????????hold();
13?????????release();
14?????}
15?
16?????public?static?void?hold()?{
17?????????System.out.println("--------------hold--------------");
18?????????//?Create?an?object
19?????????Student?stu?=?new?Student(0,?"kobe",?33);
20?????????//?Create?a?reference?queue
21?????????ReferenceQueue<Student>?rq?=?new?ReferenceQueue<Student>();
22?????????//?Create?a?weakReference?to?obj?and?associate?our?reference?queue
23?????????WeakReference<Student>?wr?=?new?WeakReference<Student>(stu,?rq);
24?????????System.out.println("weak?reference?wr:?"?+?wr);?//?wr
25?????????System.out.println("weak?reference?stu?wr.get():?"?+?wr.get());?//?stu
26?????????//?Check?to?see?if?it′s?on?the?ref?queue?yet
27?????????System.out.println("Polling?rq.poll():?"?+?rq.poll());?//?null
28?????????System.gc();
29?????????System.out.println("hold???gc???");
30?????????System.out.println("Polling??rq.poll():?"?+?rq.poll());?//?null
31?????????System.out.println("weak?reference?returns?obj?wr.get():?"?+?wr.get());?//?not?null
32?????}
33?
34?????public?static?void?release()?{
35?????????System.out.println("--------------release--------------");
36?????????//?Create?an?object
37?????????Student?stu?=?new?Student(0,?"劉德華",?48);
38?????????//?Create?a?reference?queue
39?????????ReferenceQueue<Student>?rq?=?new?ReferenceQueue<Student>();
40?????????//?Create?a?weakReference?to?obj?and?associate?our?reference?queue
41?????????WeakReference<Student>?wr?=?new?WeakReference<Student>(stu,?rq);
42?????????System.out.println("weak?reference?wr:?"?+?wr);?//?wr
43?????????System.out.println("weak?reference?stu?wr.get():?"?+?wr.get());?//?stu
44?????????//?Check?to?see?if?it′s?on?the?ref?queue?yet
45?????????System.out.println("Polling?rq.poll():?"?+?rq.poll());?//?null
46?????????stu?=?null;
47?????????System.gc();
48?????????System.out.println("release???gc???");
49?????????System.out.println("Polling??rq.poll()?:?"?+?rq.poll());?//maybe?not?null
50?????????System.out.println("weak?reference?returns?obj?:?wr.get()?"?+?wr.get());?//?null
51?????}
?
??1?package?com.test.reference.mode;
?2??3?public?class?Student?{
?4?
?5?????private?int?id;
?6?????private?String?name;
?7?????private?int?age;
?8?????
?9?????public?Student(int?id,?String?name,?int?age)?{
10?????????super();
11?????????this.id?=?id;
12?????????this.name?=?name;
13?????????this.age?=?age;
14?????}
15?????
16?????@Override
17?????protected?void?finalize()?throws?Throwable?{
18?????????super.finalize();
19?????????System.out.println("finalize?:?"?+?toString());
20?????}
21?????
22?????@Override
23?????public?String?toString()?{
24?????????return?"?[Student?msg?:?id?=?"?+?id?+?"?name?=?"?+?name?+?"?age?=?"?+?age?+"]";
25?????}
26?????
27?????public?int?getId()?{
28?????????return?id;
29?????}
30?????public?void?setId(int?id)?{
31?????????this.id?=?id;
32?????}
33?????public?String?getName()?{
34?????????return?name;
35?????}
36?????public?void?setName(String?name)?{
37?????????this.name?=?name;
38?????}
39?????public?int?getAge()?{
40?????????return?age;
41?????}
42?????public?void?setAge(int?age)?{
43?????????this.age?=?age;
44?????}
45?????
46?}
?
?1?package?com.test.reference;?2?
?3?import?java.lang.ref.ReferenceQueue;
?4?import?java.lang.ref.WeakReference;
?5?
?6?import?com.test.reference.mode.Student;
?7?
?8?
?9?
10?public?class?ReferenceUsage?{
11?????public?static?void?main(String?args[])?{
12?????????hold();
13?????????release();
14?????}
15?
16?????public?static?void?hold()?{
17?????????System.out.println("--------------hold--------------");
18?????????//?Create?an?object
19?????????Student?stu?=?new?Student(0,?"kobe",?33);
20?????????//?Create?a?reference?queue
21?????????ReferenceQueue<Student>?rq?=?new?ReferenceQueue<Student>();
22?????????//?Create?a?weakReference?to?obj?and?associate?our?reference?queue
23?????????WeakReference<Student>?wr?=?new?WeakReference<Student>(stu,?rq);
24?????????System.out.println("weak?reference?wr:?"?+?wr);?//?wr
25?????????System.out.println("weak?reference?stu?wr.get():?"?+?wr.get());?//?stu
26?????????//?Check?to?see?if?it′s?on?the?ref?queue?yet
27?????????System.out.println("Polling?rq.poll():?"?+?rq.poll());?//?null
28?????????System.gc();
29?????????System.out.println("hold???gc???");
30?????????System.out.println("Polling??rq.poll():?"?+?rq.poll());?//?null
31?????????System.out.println("weak?reference?returns?obj?wr.get():?"?+?wr.get());?//?not?null
32?????}
33?
34?????public?static?void?release()?{
35?????????System.out.println("--------------release--------------");
36?????????//?Create?an?object
37?????????Student?stu?=?new?Student(0,?"劉德華",?48);
38?????????//?Create?a?reference?queue
39?????????ReferenceQueue<Student>?rq?=?new?ReferenceQueue<Student>();
40?????????//?Create?a?weakReference?to?obj?and?associate?our?reference?queue
41?????????WeakReference<Student>?wr?=?new?WeakReference<Student>(stu,?rq);
42?????????System.out.println("weak?reference?wr:?"?+?wr);?//?wr
43?????????System.out.println("weak?reference?stu?wr.get():?"?+?wr.get());?//?stu
44?????????//?Check?to?see?if?it′s?on?the?ref?queue?yet
45?????????System.out.println("Polling?rq.poll():?"?+?rq.poll());?//?null
46?????????stu?=?null;
47?????????System.gc();
48?????????System.out.println("release???gc???");
49?????????System.out.println("Polling??rq.poll()?:?"?+?rq.poll());?//maybe?not?null
50?????????System.out.println("weak?reference?returns?obj?:?wr.get()?"?+?wr.get());?//?null
51?????}
52?}?
?
運行結果:
--------------hold--------------weak reference wr: java.lang.ref.WeakReference@61de33weak reference stu wr.get(): ?[Student msg : id = 0 name = kobe age = 33]Polling rq.poll(): nullhold ? gc ??Polling ?rq.poll(): nullweak reference returns obj wr.get(): ?[Student msg : id = 0 name = kobe age = 33]--------------release--------------weak reference wr: java.lang.ref.WeakReference@ca0b6weak reference stu wr.get(): ?[Student msg : id = 0 name = 劉德華 age = 48]Polling rq.poll(): nullfinalize : ?[Student msg : id = 0 name = 劉德華 age = 48]finalize : ?[Student msg : id = 0 name = kobe age = 33]release ? gc ??Polling ?rq.poll() : java.lang.ref.WeakReference@ca0b6weak reference returns obj : wr.get() null?
?
?
轉載于:https://www.cnblogs.com/cody1988/archive/2012/04/18/2455299.html
總結
以上是生活随笔為你收集整理的java点滴(6)之java引用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python单元测试框架Pyunit 的
- 下一篇: 第十五章 五虎上将中谁最冷血