在Java中,有四种类型的引用根据垃圾收集的方式进行区分。
null
- 有力的证明
- 弱引用
- 软引用
- 幻影参考
先决条件: 垃圾收集
- 强有力的参考: 这是引用对象的默认类型/类别。任何具有活动强引用的对象都不符合垃圾收集的条件。仅当强引用的变量指向null时,才会对对象进行垃圾收集。
MyClass obj = new MyClass ();
这里的“obj”对象是对新创建的MyClass实例的强引用,目前obj是活动对象,所以不能被垃圾收集。
obj = null; //'obj' object is no longer referencing to the instance. So the 'MyClass type object is now available for garbage collection.
// Java program to illustrate Strong reference
class
Gfg
{
//Code..
}
public
class
Example
{
public
static
void
main(String[] args)
{
//Strong Reference - by default
Gfg g =
new
Gfg();
//Now, object to which 'g' was pointing earlier is
//eligible for garbage collection.
g =
null
;
}
}
- 弱引用: 弱引用对象不是引用对象的默认类型/类,在使用它们时应该显式指定它们。
- 这种类型的引用在WeakHashMap中用于引用条目对象。
- 如果JVM检测到一个对象只有弱引用(即没有链接到任何对象的强引用或软引用),该对象将被标记为垃圾收集。
- 创建这样的引用 JAVAlang.ref.WeakReference 使用类。
- 这些引用在实时应用程序中使用,同时建立DBConnection,当使用数据库的应用程序关闭时,垃圾收集器可能会清除DBConnection。
//Java Code to illustrate Weak reference
import
java.lang.ref.WeakReference;
class
Gfg
{
//code
public
void
x()
{
System.out.println(
"GeeksforGeeks"
);
}
}
public
class
Example
{
public
static
void
main(String[] args)
{
// Strong Reference
Gfg g =
new
Gfg();
g.x();
// Creating Weak Reference to Gfg-type object to which 'g'
// is also pointing.
WeakReference<Gfg> weakref =
new
WeakReference<Gfg>(g);
//Now, Gfg-type object to which 'g' was pointing earlier
//is available for garbage collection.
//But, it will be garbage collected only when JVM needs memory.
g =
null
;
// You can retrieve back the object which
// has been weakly referenced.
// It successfully calls the method.
g = weakref.get();
g.x();
}
}
输出:
GeeksforGeeks GeeksforGeeks
有两种不同程度的软弱:软弱和虚幻
- 软参考: 在软引用中,即使对象可以进行垃圾收集,也不会进行垃圾收集,直到JVM严重需要内存。当JVM耗尽内存时,对象将从内存中清除。创建这样的引用 JAVAlang.ref.SoftReference 使用类。
//Code to illustrate Soft reference
import
java.lang.ref.SoftReference;
class
Gfg
{
//code..
public
void
x()
{
System.out.println(
"GeeksforGeeks"
);
}
}
public
class
Example
{
public
static
void
main(String[] args)
{
// Strong Reference
Gfg g =
new
Gfg();
g.x();
// Creating Soft Reference to Gfg-type object to which 'g'
// is also pointing.
SoftReference<Gfg> softref =
new
SoftReference<Gfg>(g);
// Now, Gfg-type object to which 'g' was pointing
// earlier is available for garbage collection.
g =
null
;
// You can retrieve back the object which
// has been weakly referenced.
// It successfully calls the method.
g = softref.get();
g.x();
}
}
输出:
GeeksforGeeks GeeksforGeeks
- 幻影参考: 虚引用引用的对象符合垃圾收集的条件。但是,在从内存中删除它们之前,JVM会将它们放入一个名为“引用队列”的队列中。在对它们调用finalize()方法后,它们被放入引用队列。创建这样的引用 JAVAlang.ref.PhantomReference 使用类。
//Code to illustrate Phantom reference
import
java.lang.ref.*;
class
Gfg
{
//code
public
void
x()
{
System.out.println(
"GeeksforGeeks"
);
}
}
public
class
Example
{
public
static
void
main(String[] args)
{
//Strong Reference
Gfg g =
new
Gfg();
g.x();
//Creating reference queue
ReferenceQueue<Gfg> refQueue =
new
ReferenceQueue<Gfg>();
//Creating Phantom Reference to Gfg-type object to which 'g'
//is also pointing.
PhantomReference<Gfg> phantomRef =
null
;
phantomRef =
new
PhantomReference<Gfg>(g,refQueue);
//Now, Gfg-type object to which 'g' was pointing
//earlier is available for garbage collection.
//But, this object is kept in 'refQueue' before
//removing it from the memory.
g =
null
;
//It always returns null.
g = phantomRef.get();
//It shows NullPointerException.
g.x();
}
}
运行时错误:
Exception in thread "main" java.lang.NullPointerException at Example.main(Example.java:31)
输出:
GeeksforGeeks
本文由 普拉蒂克·阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END