垃圾内存支持的java编码标准

发布于 2024-10-21 17:57:26 字数 740 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-10-28 17:57:27

为什么不阅读有关 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 of getObject(), 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 once getObject() 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 like Object o = aSimpleClass.getObject().

時窥 2024-10-28 17:57:27

让我们来看看。

class simpleClass {
   static int st = 0;

st 变量根本没有被使用——这是故意的吗?

   public Integer getObject(){
          Interger i = 10;
          Interger j = 10;
          Interger result = 10;

我想这些应该是Integer。因此,您在这里创建了 3 个变量,并将它们分配给同一个对象,即 Integer.valueOf(10) 的结果。此方法保证对于相同的输入返回相同的对象,至少对于较小的值。我认为,它通常将这些值存储在 Integer 类的静态数组中。

          try{    
              result = getSum(i, j);

在这里,您调用 getSum 方法 - 该方法(如果已更正)返回另一个对象,该对象与 Integer.valueOf(20) 相同。

          }catch(Exception ex){
              ex.printStackTrace();

如果出现异常(如果没有重写子类中的 sum 方法,则不会),您将在此处捕获并打印异常对象,然后将其丢弃。

因此,一个对象(及其包含的堆栈跟踪对象)可用于垃圾回收。

          } finally{
              i = null;

此处,您将 null 分配给 i 变量。如果 i 包含仅通过 i 引用的对象,那么它现在可用于垃圾回收,但由于它也在 j 中变量(据说也在 Integer 中的静态变量中的某个位置),但事实并非如此。

          }
          return result;

我们结束了该方法 - 所以现在它的所有局部变量(ijresult)都结束了。返回 20 对象(在 result 中),因此现在由调用方法负责。 10 对象(在 j 中)不再被该线程引用(至少在我们看到的代码中没有),因此它可以被释放 - 但如上所述,通常Integer 类保留对此对象的引用,因此这里没有垃圾回收。

   }
   public Interger getSum(Interger a, Interger b){
         retrun a  + b;
   }

请在发布代码时至少先通过编译器运行它。它是Integerreturn

}

Let's see.

class simpleClass {
   static int st = 0;

The st variable is not used at all - is this intentional?

   public Integer getObject(){
          Interger i = 10;
          Interger j = 10;
          Interger result = 10;

I suppose these should be Integer. So, here you are creating 3 variables, and assign them all the same object, namely the result from Integer.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.

          try{    
              result = getSum(i, j);

Here you invoke the getSum method - this (if corrected) returns another object, which is the same as Integer.valueOf(20).

          }catch(Exception ex){
              ex.printStackTrace();

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.

          } finally{
              i = null;

Here you are assigning null to the i variable. If i contained an object which was only referenced through i, it would now be available for garbage collection, but since it is also in the j variable (and supposedly also somewhere in a static variable in Integer), it isn't.

          }
          return result;

We end the method - so now all its local variables (i, j , result) are ended. The 20 object (in result) is returned, thus now the responsibility of the calling method. The 10 object (in j) is not anymore referenced from this thread (at least not in the code we see), so it could be freed - but as said, usually the Integer class keeps a reference to this object, so no garbage collection here.

   }
   public Interger getSum(Interger a, Interger b){
         retrun a  + b;
   }

Please, when posting code, at least run it through a compiler before. It's Integer and return.

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文