<small id='nwmN9'></small><noframes id='nwmN9'>

      <i id='nwmN9'><tr id='nwmN9'><dt id='nwmN9'><q id='nwmN9'><span id='nwmN9'><b id='nwmN9'><form id='nwmN9'><ins id='nwmN9'></ins><ul id='nwmN9'></ul><sub id='nwmN9'></sub></form><legend id='nwmN9'></legend><bdo id='nwmN9'><pre id='nwmN9'><center id='nwmN9'></center></pre></bdo></b><th id='nwmN9'></th></span></q></dt></tr></i><div id='nwmN9'><tfoot id='nwmN9'></tfoot><dl id='nwmN9'><fieldset id='nwmN9'></fieldset></dl></div>

        <bdo id='nwmN9'></bdo><ul id='nwmN9'></ul>
      1. <tfoot id='nwmN9'></tfoot>
        <legend id='nwmN9'><style id='nwmN9'><dir id='nwmN9'><q id='nwmN9'></q></dir></style></legend>
      2. 内部类与静态嵌套类的 GC 性能下降

        时间:2023-07-13
          <tbody id='LINwh'></tbody>
          • <bdo id='LINwh'></bdo><ul id='LINwh'></ul>
          • <small id='LINwh'></small><noframes id='LINwh'>

              1. <legend id='LINwh'><style id='LINwh'><dir id='LINwh'><q id='LINwh'></q></dir></style></legend>
                <i id='LINwh'><tr id='LINwh'><dt id='LINwh'><q id='LINwh'><span id='LINwh'><b id='LINwh'><form id='LINwh'><ins id='LINwh'></ins><ul id='LINwh'></ul><sub id='LINwh'></sub></form><legend id='LINwh'></legend><bdo id='LINwh'><pre id='LINwh'><center id='LINwh'></center></pre></bdo></b><th id='LINwh'></th></span></q></dt></tr></i><div id='LINwh'><tfoot id='LINwh'></tfoot><dl id='LINwh'><fieldset id='LINwh'></fieldset></dl></div>
                  <tfoot id='LINwh'></tfoot>
                  本文介绍了内部类与静态嵌套类的 GC 性能下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我刚刚遇到了一个奇怪的效果,在跟踪它时,我注意到收集内部嵌套类与静态嵌套类似乎存在很大的性能差异.考虑这个代码片段:

                  I just came across a weird effect and while tracking it down, I noticed that there seems to be a substantial performance difference for collecting inner vs. static nested classes. Consider this code fragment:

                  public class Test {
                      private class Pointer {
                          long data;
                          Pointer next;
                      }
                      private Pointer first;
                  
                      public static void main(String[] args) {
                          Test t = null;
                          for (int i = 0; i < 500; i++) {
                              t = new Test();
                              for (int j = 0; j < 1000000; j++) {
                                  Pointer p = t.new Pointer();
                                  p.data = i*j;
                                  p.next = t.first;
                                  t.first = p;
                              }
                          }
                      }
                  }
                  

                  所以代码所做的是使用内部类创建一个链表.该过程重复 500 次(出于测试目的),丢弃上次运行中使用的对象(这些对象会受到 GC).

                  So what the code does is create a linked list using an inner class. The process is repeated 500 times (for testing purposes), discarding the objects used in the last run (which become subject to GC).

                  当以严格的内存限制(如 100 MB)运行时,此代码在我的机器上执行大约需要 20 分钟.现在,通过简单地用静态嵌套类替换内部类,我可以将运行时间减少到 6 分钟以内.以下是更改:

                  When run with a tight memory limit (like 100 MB), this code takes about 20 minutes to execute on my machine. Now, by simply replacing the inner class with a static nested class, I can reduce the runtime to less than 6 minutes. Here are the changes:

                      private static class Pointer {
                  

                                  Pointer p = new Pointer();
                  

                  现在我从这个小实验中得出的结论是,使用内部类使 GC 更难以确定是否可以收集对象,在这种情况下,静态嵌套类的速度提高了 3 倍以上.

                  Now my conclusions from this little experiment are that using inner classes makes it much more difficult for the GC to figure out if the objects can be collected, making static nested classes more than 3x faster in this case.

                  我的问题是这个结论是否正确;如果是,那是什么原因,如果不是,为什么内部类在这里这么慢?

                  My question is if this conclusion is correct; if yes what is the reason, and if no why are inner classes so much slower here?

                  推荐答案

                  我想这是由于两个因素.你已经提到的第一个.第二个是使用非静态内部类会导致更多的内存使用.你为什么问?因为非静态内部类也可以访问它们包含的类数据成员和方法,这意味着您正在分配一个基本上扩展超类的 Pointer 实例.在非静态内部类的情况下,您不会扩展包含类.这是我正在谈论的一个例子

                  I would imagine this is due to 2 factors. The first one you already touched upon. The second is using non-static inner classes results in more memory usage. Why you ask? Because non-static inner classes also have access to their containing classes data members and methods, which means you are allocating a Pointer instance that basically extends the superclass. In the case of non-static inner classes you are not extending the containing class. Here is an example of what I'm talking about

                  Test.java(非静态内部类)

                  Test.java (non-static inner class)

                  public class Test {
                      private Pointer first;
                  
                      private class Pointer {
                          public Pointer next;
                          public Pointer() {
                              next = null;
                          }
                      }
                  
                      public static void main(String[] args) {
                          Test test = new Test();
                          Pointer[] p = new Pointer[1000];
                          for ( int i = 0; i < p.length; ++i ) {
                              p[i] = test.new Pointer();
                          }
                  
                          while (true) {
                              try {Thread.sleep(100);}
                              catch(Throwable t) {}
                          }
                      }
                  }
                  

                  Test2.java(静态内部类)

                  Test2.java (static inner class)

                  public class Test2 {
                      private Pointer first;
                  
                      private static class Pointer {
                          public Pointer next;
                          public Pointer() {
                              next = null;
                          }
                      }
                  
                      public static void main(String[] args) {
                          Test test = new Test();
                          Pointer[] p = new Pointer[1000];
                          for ( int i = 0; i < p.length; ++i ) {
                              p[i] = new Pointer();
                          }
                  
                          while (true) {
                              try {Thread.sleep(100);}
                              catch(Throwable t) {}
                          }
                      }
                  }
                  

                  当两者都运行时,您可以看到非静态比静态占用更多的堆空间.具体来说,非静态版本使用 2,279,624 B,静态版本使用 10,485,760 1,800,000 B.

                  When both are run you can see the non-static taking up more heap space than the static. Specifically, the non-static version used 2,279,624 B and the static version used 10,485,760 1,800,000 B.

                  因此,归结为非静态内部类使用更多内存,因为它包含(至少)对包含类的引用.静态内部类不包含此引用,因此永远不会为其分配内存.通过将堆大小设置得如此之低,实际上是在破坏堆,从而导致 3 倍的性能差异.

                  So, what it comes down to is the non-static inner class uses more memory because it contains a reference (at the very least) to the containing class. The static inner class doesn't contain this reference so the memory is never allocated for it. By setting your heap size so low you were actually thrashing your heap, which resulted in the 3x performance difference.

                  这篇关于内部类与静态嵌套类的 GC 性能下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 Java 堆的最大大小是固定的? 下一篇:System.getenv() &amp; 之间的区别System.getProperty()

                  相关文章

                    <bdo id='wR3qm'></bdo><ul id='wR3qm'></ul>
                1. <small id='wR3qm'></small><noframes id='wR3qm'>

                    <i id='wR3qm'><tr id='wR3qm'><dt id='wR3qm'><q id='wR3qm'><span id='wR3qm'><b id='wR3qm'><form id='wR3qm'><ins id='wR3qm'></ins><ul id='wR3qm'></ul><sub id='wR3qm'></sub></form><legend id='wR3qm'></legend><bdo id='wR3qm'><pre id='wR3qm'><center id='wR3qm'></center></pre></bdo></b><th id='wR3qm'></th></span></q></dt></tr></i><div id='wR3qm'><tfoot id='wR3qm'></tfoot><dl id='wR3qm'><fieldset id='wR3qm'></fieldset></dl></div>
                  1. <legend id='wR3qm'><style id='wR3qm'><dir id='wR3qm'><q id='wR3qm'></q></dir></style></legend><tfoot id='wR3qm'></tfoot>