概述
想比与C/C++可以直接操作内存的语言,java引入了GC机制,但实际情况中仍需控制对象GC等级于是在JDK 1.2 引入了四种引用:强引用,软引用,弱引用,虚引用,来帮助GC更精确的释放对象的内存。本文章所使用的jvm参数 -Xms1000M。
强引用
强引用无需引入其他实体类,所引用的对象为 若该对象被清理将导致程序无法进行的对象。也就是平常最常使用的引用。JVM宁愿抛出oom异常,也不会尝试回收所关联的对象。使用形式如下:
Object object=new Object();
软引用
软引用需引入java.lang.ref.SoftReference类,所引用的对象为 仍有用但非必须的对象。被软引用关联的对象,将在抛出oom异常之前回收。可以应用为某些缓存内容,加快程序速度,同时又不影响内存使用。使用形式如下:
Object object=new Object();
SoftReference aSoftRef=new SoftReference(object);
aSoftRef.get();
软引用demo
public class Refqueue {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws InterruptedException {
//创建软引用
ReferenceQueue<SoftReference<G>> rq = new ReferenceQueue<SoftReference<G>>();
SoftReference[] srArr = new SoftReference[1000];
for(int i = 0; i < srArr.length; i++){
srArr[i] = new SoftReference(new G(), rq);
}
//获取被清除部分
int n=0;
for(int i = 0; i < srArr.length; i++){
if(srArr[i].isEnqueued()){
srArr[i]=null;
n++;
}
}
System.out.println("第一次GC,清除了"+n+"个");
//尝试请求一次GC
System.gc();
//获取第二次被清除部分
for(int i=0;i<10000;i++){
G g=new G();
}
int m=0;
for(int i = 0; i < srArr.length; i++){
if(srArr[i]!=null&&srArr[i].isEnqueued()){
srArr[i]=null;
m++;
}
}
System.out.println("第一次GC,清除了"+m+"个");
}
}
//为了占据内存
class G{
private int [] big=new int[1000000];
}
/*
output:
第一次GC,清除了753个
第一次GC,清除了0个
*/
弱引用
弱引用需引入java.lang.ref.WeakReference类,弱引用与软引用类似都用于引用 非必需对象,但是弱引用的生存时间更短,仅在下次GC之前。使用形式如下:
Object object=new Object();
WeakReference aWeakRef=new WeakReference(object);
aWeakRef.get();
弱引用demo
public class Refqueue {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws InterruptedException {
//创建弱引用
ReferenceQueue<WeakReference<G>> rq = new ReferenceQueue<WeakReference<G>>();
WeakReference[] srArr = new WeakReference[1000];
for(int i = 0; i < srArr.length; i++){
srArr[i] = new WeakReference(new G(), rq);
}
//获取被清除部分
int n=0;
for(int i = 0; i < srArr.length; i++){
if(srArr[i].isEnqueued()){
srArr[i]=null;
n++;
}
}
System.out.println("第一次GC,清除了"+n+"个");
//尝试请求一次GC
System.gc();
//获取第二次被清除部分
int m=0;
for(int i = 0; i < srArr.length; i++){
if(srArr[i]!=null&&srArr[i].isEnqueued()){
srArr[i]=null;
m++;
}
}
System.out.println("第一次GC,清除了"+m+"个");
}
}
//为了占据内存
class G{
private int [] big=new int[1000000];
}
/*
output (第二次清除个数有明显变动)
第一次GC,清除了965个
第一次GC,清除了16个
*/
虚引用
虚引用与前面三种引用不同,并不是为了程序员干预对象的GC优先级。而是为了更精细的控制对象内存的释放,必须与引用队列一同使用,当对象引用被释放时,其对象仍存在内存中,并未被释放,对象此时加入队列中,等待执行finalize函数。同时我们需要重写对象的finalize函数,帮助其释放内存。注意:使用.get()方法是获取不到对象的。
虚引用demo
public class Refqueue {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws InterruptedException {
//创建弱引用
ReferenceQueue<PhantomReference<G>> rq = new ReferenceQueue<PhantomReference<G>>();
PhantomReference[] srArr = new PhantomReference[1000];
for(int i = 0; i < srArr.length; i++){
G g=new G();
srArr[i] = new PhantomReference(g, rq);
//g = null;
}
//获取被清除部分
int n = 0;
for(int i = 0; i < srArr.length; i++){
if(srArr[i].isEnqueued()){
srArr[i] = null;
n++;
}
}
System.out.println("清除了"+n+"个");
}
}
//为了占据内存
class G{
private int [] big=new int[1000000];
@Override
protected void finalize() throws Throwable {
super.finalize();
big=null;
}
}
/*
output
清除了826个
*/
总结
引用与GC的等级关系(依照小鱼吃大鱼):弱引用->每次GC->软引用->oom异常->强引用。虚引用目的不同,不在等级关系之内。