运行以下代码后,有多少对象可用于垃圾回收? [英] How many objects are available for garbage collection after the following code is runned?

查看:69
本文介绍了运行以下代码后,有多少对象可用于垃圾回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 class beta {
}
class demo {
static beta b1;
beta b2;
public static void main(String args []){
beta b1 = new beta();
beta b2 = new beta();
demo d1 = new demo();
demo d2 = new demo();
d1.b1 = b1;
d1.b2 = b1;
d2.b2 = b2;
d1 = null;
b1 = null;
b2 = null;

}
}





如果我们添加System.out.println(d1.b1) ;在主要区块内,然后输出是beta @ 3a7f1228

,即;不是空的。



我尝试了什么:



我们可以仍然访问d1.b1所以d1的对象仍然存在,因此没有一个对gc可用。

告诉我我错在哪里?

答案是1. div class =h2_lin>解决方案

您已将 b1 声明为静态,因此它始终存在。


请停止询问此类问题。



如前所述, main()方法结束,因为程序终止并且内存中没有主动引用的对象。


class beta{
}
class demo{
	static beta b1;
	beta b2;
	public static void main(String args[]){
		beta b1 = new beta();
		beta b2 = new beta();
		demo d1 = new demo();
		demo d2 = new demo();
		d1.b1=b1;
		d1.b2=b1;
		d2.b2=b2;
		d1= null;
		b1= null;
		b2=null;
	
	}
}



If we add System.out.println(d1.b1); inside main block then the output is beta@3a7f1228
i.e; not null.

What I have tried:

As we can still access d1.b1 so object of d1 is still alive, thus none should be available for gc.
Tell me where i am wrong?
The answer is 1.

解决方案

You have declared b1 as static so it always exists.


Please stop asking this type of question.

As already stated all the objects are available for garbage collection after the main() method ends since the program terminates and there is no actively referenced objects in memory.


这篇关于运行以下代码后,有多少对象可用于垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆