垃圾内存支持的java编码标准
simpleClass sc = new simpleClass();
sc.getObject();
...
simpleClass.java
class simpleClass {
static int st = 0;
public Integer getObject() {
Integer i = 10;
Integer j = 10;
Integer result = 10;
try {
result = getSum(i, j);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
i = null;
}
return result;
}
public Integer getSum(Integer a, Integer b) {
return a + b;
}
}
请解释哪些变量/对象符合垃圾标准以及何时?
哪些内容会保留在内存中直到什么时间?
对于符合垃圾内存条件但仍在内存中的对象该怎么办?
直到记忆中,才是生活和印象。
我和我的朋友们已经为此进行了辩论,并且仍在进行许多不同的声明。
simpleClass sc = new simpleClass();
sc.getObject();
...
simpleClass.java
class simpleClass {
static int st = 0;
public Integer getObject() {
Integer i = 10;
Integer j = 10;
Integer result = 10;
try {
result = getSum(i, j);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
i = null;
}
return result;
}
public Integer getSum(Integer a, Integer b) {
return a + b;
}
}
Please explain which variable/Object are eligible for Garbage and when?
which stay in memory until what time?
what to do for object that is eligible for Garbage memory but still in memory?
Untill when in memory, is the life and impression.
my friends and i already debated for this and still going on with so many different Statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不阅读有关 Java 垃圾收集的一些内容而不是争论:)?例如 http://www.ibm.com/developerworks/java/library/ j-jtp11253/ 或 http:// java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html 。
那里有所谓的根集的定义,它本质上由静态引用组成(您的代码中没有静态引用,因为
static int
不是引用)或执行期间作为局部变量(Integer i
/Integer j
/Integer result
)以及参数(Integer a/ Integer b)的堆栈变量一种方法。因此,在执行 getObject() 期间,i、j 和结果引用的对象将成为根集的一部分,并且不符合 gc 条件(以及那些可能间接引用的任何对象) )。然而,一旦getObject()
完成,i 和 j 的对象就可以被删除。如果将结果对象分配给另一个局部变量,即如果您执行类似Object o = aSimpleClass.getObject()
的操作,结果对象将保留在根集中。Why not read up on some things about Java garbage collection instead of arguing :)? For instance http://www.ibm.com/developerworks/java/library/j-jtp11253/ or http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html .
There are definitions there for the so called root set, which essentially consists of static references (which you have none of in your code, as
static int
is not a reference) or stack variables which are locals (Integer i
/Integer j
/Integer result
) as well as parameters (Integer a/ Integer b) during execution of a method. So during the execution ofgetObject()
, the objects referenced by i, j ans result will be part of the root set and not eligible for gc (as well as any objects that might be indirectly referenced by those). Once oncegetObject()
finishes however, i's and j's objects can be removed. The result object will stay in the root set, if its assigned to another local variable, i.e. if you do something likeObject o = aSimpleClass.getObject()
.让我们来看看。
st
变量根本没有被使用——这是故意的吗?我想这些应该是
Integer
。因此,您在这里创建了 3 个变量,并将它们分配给同一个对象,即Integer.valueOf(10)
的结果。此方法保证对于相同的输入返回相同的对象,至少对于较小的值。我认为,它通常将这些值存储在 Integer 类的静态数组中。在这里,您调用
getSum
方法 - 该方法(如果已更正)返回另一个对象,该对象与Integer.valueOf(20)
相同。如果出现异常(如果没有重写子类中的 sum 方法,则不会),您将在此处捕获并打印异常对象,然后将其丢弃。
因此,一个对象(及其包含的堆栈跟踪对象)可用于垃圾回收。
此处,您将
null
分配给i
变量。如果i
包含仅通过i
引用的对象,那么它现在可用于垃圾回收,但由于它也在j
中变量(据说也在Integer
中的静态变量中的某个位置),但事实并非如此。我们结束了该方法 - 所以现在它的所有局部变量(
i
,j
,result
)都结束了。返回20
对象(在result
中),因此现在由调用方法负责。10
对象(在j
中)不再被该线程引用(至少在我们看到的代码中没有),因此它可以被释放 - 但如上所述,通常Integer
类保留对此对象的引用,因此这里没有垃圾回收。请在发布代码时至少先通过编译器运行它。它是
Integer
和return
。Let's see.
The
st
variable is not used at all - is this intentional?I suppose these should be
Integer
. So, here you are creating 3 variables, and assign them all the same object, namely the result fromInteger.valueOf(10)
. This method is guaranteed to return the same object for the same input, at least for small values. It usually stores these values in a static array in the Integer class, I think.Here you invoke the
getSum
method - this (if corrected) returns another object, which is the same asInteger.valueOf(20)
.If there was an exception (it is not, if you don't override the sum method in a subclass), you would here catch and print the exception object, and then throw it away.
So, one object (and its contained stacktrace objects) available for garbage collection.
Here you are assigning
null
to thei
variable. Ifi
contained an object which was only referenced throughi
, it would now be available for garbage collection, but since it is also in thej
variable (and supposedly also somewhere in a static variable inInteger
), it isn't.We end the method - so now all its local variables (
i
,j
,result
) are ended. The20
object (inresult
) is returned, thus now the responsibility of the calling method. The10
object (inj
) is not anymore referenced from this thread (at least not in the code we see), so it could be freed - but as said, usually theInteger
class keeps a reference to this object, so no garbage collection here.Please, when posting code, at least run it through a compiler before. It's
Integer
andreturn
.