1. JVM运行环境中垃圾对象的定义
2.堆内存
*在JVM启动时被创建;堆内存中所存储的对象可以被JVM自动回收,不能通过其他外部手段回收
* 堆内存可分为两个区域:新对象区和老对象区
3.JVM中对象的生命周期
当内存不足时,JVM宁愿抛出OutOfMemeryError错误使程序停止,也不会靠收回具有强引用的对象来释放内存空间
软引用可以和一个引用队列(ReferenceQueue)联合使用,如果软引用所引用的对象被垃圾回收,java虚拟机就会把这个软引用加入到与之关联的引用队列中。
- import
java.lang.ref.SoftReference - //实现cache功能,最大限度利用内存
- Test
test = new Test(); - SoftReference
sr = new SoftRefence(test); - test
= null; - if(sr.get()
!= null){ -
test = sr.get(); - }else{
-
test = new Test(); -
sr = new SoftReference(test); -
test = null; - }
- <span
style="font-size: small;">import java.lang.ref.SoftReference - //实现cache功能,最大限度利用内存
- Test
test = new Test(); - SoftReference
sr = new SoftRefence(test); - test
= null; - if(sr.get()
!= null){ -
test = sr.get(); - }else{
-
test = new Test(); -
sr = new SoftReference(test); -
test = null; - }</span>
- //创建一个强引用
- String
str = new String("hello"); - //创建引用队列,
<String>为范型标记,表明队列中存放String对象的引用 - ReferenceQueue<String>
rq = new ReferenceQueue<String>(); - //创建一个弱引用,它引用"hello"对象,并且与rq引用队列关联
- //<String>为范型标记,表明WeakReference会弱引用String对象
- SoftReference<String>
wf = new SoftReference<String>(str, rq); - str=null;
//取消"hello"对象的强引用 - String
str1=wf.get(); //假如"hello"对象没有被回收,str1引用"hello"对象 - //假如"hello"对象没有被回收,rq.poll()返回null
- Reference<?
extends String> ref=rq.poll();
- <span
style="font-size: small;">//创建一个强引用 - String
str = new String("hello"); - //创建引用队列,
<String>为范型标记,表明队列中存放String对象的引用 - ReferenceQueue<String>
rq = new ReferenceQueue<String>(); - //创建一个弱引用,它引用"hello"对象,并且与rq引用队列关联
- //<String>为范型标记,表明WeakReference会弱引用String对象
- SoftReference<String>
wf = new SoftReference<String>(str, rq); - str=null;
//取消"hello"对象的强引用 - String
str1=wf.get(); //假如"hello"对象没有被回收,str1引用"hello"对象 - //假如"hello"对象没有被回收,rq.poll()返回null
- Reference<?
extends String> ref=rq.poll(); - </span>
- package
reference; -
- import
java.util.*; - import
java.lang.ref.*; -
- class
Key { -
String id; -
public Key(String id) { -
this.id = id; -
} -
public String toString() { -
return id; -
} -
-
public int hashCode() { -
return id.hashCode(); -
} -
-
public boolean equals(Object r) { -
return (r instanceof Key) && id.equals(((Key) r).id); -
} -
-
public void finalize() { -
System.out.println("Finalizing Key " + id); -
} - }
-
- class
Value { -
String id; -
-
public Value(String id) { -
this.id = id; -
} -
-
public String toString() { -
return id; -
} -
-
public void finalize() { -
System.out.println("Finalizing Value " + id); -
} - }
-
- public
class MapCache { -
public static void main(String[] args) throws Exception { -
int size = 1000; -
// 或者从命令行获得size的大小 -
if (args.length > 0) -
size = Integer.parseInt(args[0]); -
-
Key[] keys = new Key[size]; // 存放键对象的强引用 -
WeakHashMap<Key, Value> whm = new WeakHashMap<Key, Value>(); -
for (int i = 0; i < size; i++) { -
Key k = new Key(Integer.toString(i)); -
Value v = new Value(Integer.toString(i)); -
if (i % 3 == 0) -
keys[i] = k; // 使Key对象持有强引用 -
whm.put(k, v); // 使Key对象持有弱引用 -
} -
// 催促垃圾回收器工作 -
System.gc(); -
-
// 把CPU让给垃圾回收器线程 -
Thread.sleep(8000); -
} - }
- <span
style="font-size: small;">package reference; -
- import
java.util.*; - import
java.lang.ref.*; -
- class
Key { -
String id; -
public Key(String id) { -
this.id = id; -
} -
public String toString() { -
return id; -
} -
-
public int hashCode() { -
return id.hashCode(); -
} -
-
public boolean equals(Object r) { -
return (r instanceof Key) && id.equals(((Key) r).id); -
} -
-
public void finalize() { -
System.out.println("Finalizing Key " + id); -
} - }
-
- class
Value { -
String id; -
-
public Value(String id) { -
this.id = id; -
} -
-
public String toString() { -
return id; -
} -
-
public void finalize() { -
System.out.println("Finalizing Value " + id); -
} - }
-
- public
class MapCache { -
public static void main(String[] args) throws Exception { -
int size = 1000; -
// 或者从命令行获得size的大小 -
if (args.length > 0) -
size = Integer.parseInt(args[0]); -
-
Key[] keys = new Key[size]; // 存放键对象的强引用 -
WeakHashMap<Key, Value> whm = new WeakHashMap<Key, Value>(); -
for (int i = 0; i < size; i++) { -
Key k = new Key(Integer.toString(i)); -
Value v = new Value(Integer.toString(i)); -
if (i % 3 == 0) -
keys[i] = k; // 使Key对象持有强引用 -
whm.put(k, v); // 使Key对象持有弱引用 -
} -
// 催促垃圾回收器工作 -
System.gc(); -
-
// 把CPU让给垃圾回收器线程 -
Thread.sleep(8000); -
} - }
-
- </span>
4.Java中的析构方法finalize
5. 应用能干预垃圾回收吗?
6. 垃圾回收算法
*引用计数
*标记算法