简体   繁体   English

Java匿名对象和垃圾收集-1

[英]Java Anonymous object and Garbage collection part -1

public void function(){
   new Student().setName("john");
}

public void function(){
   Student student = new Student();
   student.setName("john");
}

Does GC behave differently for both of the snip? 两种剪裁的GC行为是否不同?

I mean which case (CASE-1/CASE-2) is more GC efficient in terms of Time? 我是说哪种情况(CASE-1 / CASE-2)在时间方面更具有GC效率?

Does GC behave differently for both of the snip? 两种剪裁的GC行为是否不同?

No. Once the setName method has been invoked, the Student object created with new Student is no longer reachable and can be garbage collected. 否。一旦调用setName方法,使用new Student创建的Student对象将不再可访问,并且可以进行垃圾回收。

I mean which case (CASE-1/CASE-2) is more GC efficient in terms of Time? 我是说哪种情况(CASE-1 / CASE-2)在时间方面更具有GC效率?

Neither is more efficient. 两者都不是更有效率的。 The first case will have one less assignment in the bytecode. 第一种情况将在字节码中少分配一个。 This does not affect GC whatsoever. 这不会影响GC。

From the JLS JLS

A reachable object is any object that can be accessed in any potential continuing computation from any live thread. 可达对象是可以从任何活动线程进行任何潜在的连续计算中访问的任何对象。

In both snippets, after the invocation of setName , the Student object is no longer reachable (assuming the constructor and the setName method don't leak references to the object - but even in that case, the behavior of both snippets would be the same). 在两个代码段中,调用setNameStudent对象不再可访问(假定构造函数和setName方法不会泄漏对该对象的引用-但即使在这种情况下,两个代码段的行为也相同) 。

In the first case you don't assign the newly created object to a variable, hence it becomes unreachable for the code (and thus becomes a candidate for garbage collection) as soon as setName (String name) method returns. 在第一种情况下,您没有将新创建的对象分配给变量,因此,一旦setName (String name)方法返回,则代码将无法访问该对象(并因此成为垃圾回收的候选对象setName (String name)

In the second case local variable student will prevent the student object from being garbage collected until it goes out of scope. 在第二种情况下,局部变量student将阻止对Student对象进行垃圾收集,直到超出范围为止。 In other words, in the second snippet the student object will continue to be a live object after setName(String name) returns and will become a candidate for garbage collection only after the method function() returns. 换句话说,在第二个片段中,在setName(String name)返回之后,student对象将继续是一个活动对象,并且仅在function()方法返回之后,该对象才成为垃圾回收的候选对象。

UPDATE: 更新:

In terms of the time required for garbage collection both cases are equal since in all of them you end up having one garbage object. 就垃圾收集所需的时间而言,这两种情况是相等的,因为在所有情况下,您最终都会拥有一个垃圾对象。

Answer for question is no. 问题的答案是否定的。

Gc behaves nearly same for both cases. 两种情况下,Gc的行为几乎相同。

Garbage Collector has a unpredictable behavior. 垃圾收集器具有不可预测的行为。 But Any Object which is no longer referred or is no longer in use is eligible for garbage collection. 但是,不再引用或不再使用的任何对象都有资格进行垃圾回收。

Case 1 : Main objective of anonymous object is for instant use (one time use). 情况1:匿名对象的主要目的是即时使用(一次使用)。 So after line "new Student().setName("john");" 因此,在“ new Student()。setName(“ john”);”行之后 , your anonymous object is not in use so it will be GC. ,则您的匿名对象未被使用,因此它将是GC。

case 2 : Student student = new Student(); 情况2:学生=新学生();

student.setName("john"); student.setName( “约翰”);

After this line student reference is no longer referred so it will be GC. 在此行之后,不再引用学生参考,因此它将是GC。

There are few chances that in case 2 student reference may be leaked but GC is smart enough to handle this. 万一2号学生参考文献可能被泄露,但GC足够聪明地处理此问题的可能性很小。

Now in case 1 if you want object for one time use then go for anonymous object as Objects are created in heap memory and GC sweep heap memory. 现在,在第一种情况下,如果您希望对象使用一次,那么请使用匿名对象,因为对象是在堆内存和GC清除堆内存中创建的。 Stack memory are managed in such way that memory used by stack is reclaimed automatically. 堆栈内存的管理方式是自动回收堆栈使用的内存。

You can referred this link for more. 您可以参考此链接以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM