1. jvm中的内存区域划分
理解
public class Test {
private int n;
private static int m;
public static void main(String[] args) {
Test test = new Test();
}
}
2. jvm中的类加载机制
双亲委派模型
3.垃圾回收机制
如何进行垃圾回收
识别垃圾
func() {
{
test t = new Test();
t.start();
}
//当代码执行到这里时,局部变量t就被销毁了,
//此时new Tset()对象就没有引用在指向他了,
//此时这个代码无法使用这个对象,就被回收了
}
1. 引用计数
//伪代码
{
Test t1 = new Test();
//当代码执行到这里,t1指向new Test这个对象,引用空间计数为1 ,代表有1个引用指向这个对象
Test t2 = t1;
//当代码执行到这里,t2指向new Test这个对象,引用空间计数加1 ,代表有2个引用指向这个对象
t1 = null;
// 此时t1这个引用指向空,此时引用空间减1,此时有一个引用指向这个对象.
t2 = null;
//此时t2这个引用为空,此时引用空间减1,此时没有引用指向这个对象
//引用空间为0 ,此时可以回收这个对象.
}
class Test {
Test t;
public static void main(String[] args) {
Test a = new Test();
Test b = new Test();
a.t = b;
//此时a指向的这个对象的引用空间计数为2
b.t = a;
//此时b指向的这个对象的引用空间计数为2
a = null;
//a指向的这个对象引用空间减1,但a这个引用都指向空了
//此时引用数不为0 ,不能被回收掉,但这个对象有无法再使用了
b = null;
//b指向的这个对象引用空间减1,但b这个引用都指向空了
//此时引用数不为0 ,不能被回收掉,但这个对象有无法再使用了
}
}