对象和垃圾收集 [英] objects and garbage collection

查看:47
本文介绍了对象和垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C级

{

public static void main(String args [])

{

C c1 = new C();

C c2 = m1(c1);

C c3 = new C();

c2 = c3 ; // 6

anothermethod();

}

静态C m1(C ob1)

{

ob1 = new C();

返回ob1;

}

}


在第6行之后,有多少对象有资格进行垃圾收集?


答案:2


请告诉我是什么这是两个对象以及如何?


提前谢谢...

解决方案

我只会说一个对象可以去;由m1()方法创建的那个和先前分配给变量c2的
。为什么你认为两个物体是垃圾?


亲切的问候,


Jos



我会说只有一个对象可以去;由m1()方法创建的那个和先前分配给变量c2的
。为什么你认为两个物体是垃圾?


亲切的问候,


Jos



这也让我感到困惑。为什么c1不符合垃圾回收的条件? anotherMethod()必须是静态的,所以在方法完成后程序将退出并从内存中删除c1和指向c2和c3的指针,不是吗?


只是你知道我有在垃圾收集方面绝对没有专业知识,所以我要求出于好奇心。


-blazed


编辑:关于以前的线程,我一直认为java没有指针,只有引用,所以当使用object1 = object 2时,它总是将内存复制到一个新位置。我显然错了......


< blockquote class =post_quotes>
这也让我感到困惑。为什么c1不符合垃圾回收的条件? anotherMethod()必须是静态的,所以在方法完成后程序将退出并从内存中删除c1和指向c2和c3的指针,不是吗?



是的,但问题是在

之后,有多少对象可以被垃圾收集c2 = c3,即*之前*调用其他方法。


编辑:关于上一个线程,我一直认为java没有指针而只有引用,所以它总是复制内存当使用object1 = object 2时到新位置。我显然错了...



这里的引用和指针类似。 Jim Gossling选择使用名词

''reference''而不是''pointer'',也许是因为Java没有任何指针

算术,因为C确实如此。


事实上,OP正在使这一切变得比它实际上复杂得多。

是;只有c2之前指向的对象才能被安全删除。


亲切的问候,


Jos


class C
{
public static void main(String args[])
{
C c1 = new C();
C c2 = m1(c1);
C c3 = new C();
c2 = c3; //6
anothermethod();
}
static C m1(C ob1)
{
ob1 = new C();
return ob1;
}
}

After line 6, how many objects are eligible for garbage collection?

Ans: 2

Please tell me as to what are those two objects here and how?

Thanks in advance...

解决方案

I''d say only one object can go; the one created by the m1() method and previously
assigned to variable c2. Why do you think two objects are garbage?

kind regards,

Jos


I''d say only one object can go; the one created by the m1() method and previously
assigned to variable c2. Why do you think two objects are garbage?

kind regards,

Jos

This confuses me as well. Why is c1 not eligible for garbage collection? anotherMethod() must be static, so after the method is completed the program will exit and remove c1 and the pointer to c2 and c3 from memory, no?

Just so you know I have absolutely no expertise when it comes to garbage collection so I''m asking out of curiosity.

-blazed

Edit: And about the previous thread, I always thought java had no pointers and only references, so it always copied the memory to a new location when using object1 = object 2. I was clearly mistaken...


This confuses me as well. Why is c1 not eligible for garbage collection? anotherMethod() must be static, so after the method is completed the program will exit and remove c1 and the pointer to c2 and c3 from memory, no?

Yep, but the question was how many objects can be garbage collected just after
the assignment c2= c3, i.e. *before* that other method was called.

Edit: And about the previous thread, I always thought java had no pointers and only references, so it always copied the memory to a new location when using object1 = object 2. I was clearly mistaken...

References and pointers are similar here. Jim Gossling choose to use the noun
''reference'' instead of ''pointer'', maybe because Java doesn''t have any pointer
arithmetic as C does.

Fact remains that the OP is making this all far more complicated than it actually
is; only the object previously pointed to by c2 can be safely removed.

kind regards,

Jos


这篇关于对象和垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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