简体   繁体   English

为什么Java进程在多个循环中的速度会随之降低?

[英]Why the speed of a Java process inside multiple loops slows down as it goes?

I'm using Win 7, Java 8, on a multicore Intel workstation. 我在多核Intel工作站上使用Win 7,Java 8。 I have something like this : 我有这样的事情:

  int SampleWeeksArray[]=new int[]{23,25,33},IdArray[]=new int[]{0,1,2};

  for (int StartDay=25;StartDay<36;StartDay++)
    for (int SampleWeeks : SampleWeeksArray)
      for (int Id : IdArray)
        for (float ValueFactor=0.08f;ValueFactor<0.13f;ValueFactor+=0.01)
        {
          DataDigester dataDigester=new DataDigester();
          dataDigester.StartDay=StartDay;
          dataDigester.SampleWeeks=SampleWeeks;
          dataDigester.Id=Id;
          dataDigester.ValueFactor=ValueFactor;
          dataDigester.DoRun();
        }

Inside dataDigester.DoRun() it reads in some text data files, parse each line and do some statistics with arrays, vectors and hashmaps and it uses ThreadPoolExecutor to run multiple processes, then save the results to a file. 在dataDigester.DoRun()内部,它读取一些文本数据文件,解析每一行并使用数组,向量和哈希图进行一些统计,并使用ThreadPoolExecutor运行多个进程,然后将结果保存到文件中。

The strange thing is each single run of DoRun() takes only 10 minutes, but inside the loops, it started with 10 min. 奇怪的是,DoRun()的每次运行仅需10分钟,但是在循环内部,它的运行时间为10分钟。 but gradually slows down to 20, 30 min. 但逐渐减慢到20、30分钟。 for each DoRun(). 对于每个DoRun()。

If I stop the loop when it's taking 30 min., then start over from where I stopped, it again took 10 min. 如果我花30分钟停止了循环,然后从停下来的地方重新开始,又花了10分钟。 and eventually slows down to 20, 30 min. 并最终减慢到20、30分钟。 again. 再次。 Why ? 为什么呢

I even put in the following lines after DoRun(), but it didn't improve speed, still slows down : 我什至在DoRun()之后添加了以下几行,但它并没有提高速度,但仍然放慢了速度:

   Thread.sleep(20*1000);                     // Rest for 20 seconds
   System.gc();

Inside each loop, DataDigester dataDigester=new DataDigester() creates a new object, it should be recycled after the DoRun(), why it slowed down ? 在每个循环中,DataDigester dataDigester = new DataDigester()创建一个新对象,应该在DoRun()之后将其回收,为什么它变慢了?

I've also noticed from Windows Task Manager, the memory usage goes up as the loops keep going. 我还从Windows任务管理器中注意到,随着循环的不断进行,内存使用量增加了。 It seems Java somehow burdens itself with something and slows down, so in this case how to keep the speed at a constant of 10 min. 看来Java会以某种方式使自身负担加重并放慢速度,因此在这种情况下,如何将速度保持在恒定的10分钟。 for each run within the loops ? 对于循环内的每次运行?

It's hard to say without knowing what DataDigester is and does, but I can think of a number of possible reasons: 在不知道DataDigester是什么和做什么的情况下很难说,但是我可以想到许多可能的原因:

  1. if DataDigester.DoRun starts up a new thread - then as the number of threads goes up, the time available per thread goes down. 如果DataDigester.DoRun启动一个新线程-则随着线程数量的增加,每个线程的可用时间减少。 If it utilizes the hardware threads, then the time will go significantly up once you go from 1 to 2 threads per hardware thread. 如果它利用硬件线程,则每个硬件线程从1个线程转到2个线程后,时间将大大增加。

  2. DataDigester may have a static structure that it adds to, and which grows slower and slower the more you add into it DataDigester可能具有添加的静态结构,并且添加到其中的数量越多,增长的速度就越慢

Declare just one DataDigester and reuse that for all of your loops instead of creating thousands and thousands of them. 仅声明一个DataDigester并将其重复用于所有循环,而不是创建成千上万的循环。

int SampleWeeksArray[]=new int[]{23,25,33},IdArray[]=new int[]{0,1,2};

DataDigester dataDigester;
for (int StartDay=25;StartDay<36;StartDay++)
    for (int SampleWeeks : SampleWeeksArray)
        for (int Id : IdArray)
            for (float ValueFactor=0.08f;ValueFactor<0.13f;ValueFactor+=0.01)
            {
                dataDigester=new DataDigester();
                dataDigester.StartDay=StartDay;
                dataDigester.SampleWeeks=SampleWeeks;
                dataDigester.Id=Id;
                dataDigester.ValueFactor=ValueFactor;
                dataDigester.DoRun();
            }
        }
    }
}

That should help with your Garbage Collection issues and the timing too. 那应该可以解决您的垃圾回收问题和时间安排。

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

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