解决 java.lang.OutOfMemoryError:Java 堆空间

发布于 2024-12-02 02:13:15 字数 1783 浏览 0 评论 0原文

正如标题所示,我在线程中遇到此错误。

违规的 LOC 如下所示:

for (int i = 0; i < objectListSize; i++) {
   logger.INFO("Loop repeat: "+i+" ...", true);
   final Double discreteScore = sp.getDouble(superPeerSocket);
   final int expectedObjectIDs = sp.getInteger(superPeerSocket);
   final String discreteObjects[] = new String[expectedObjectIDs];
   for ( int j = 0; j < expectedObjectIDs; j++)
      discreteObjects[j] = sp.getString(superPeerSocket);
   htPlus.attachInitialDiscreteList2L1(discreteScore, discreteObjects); 
}

The final String discreteObjects[] declaration is where I get the error. I am running this code inside a thread. I have two threads currently active when I get this. I also tried using the MAT tool from eclipse. here is a link with some chart files inside: PLC chart files (dropbox URL)
If anyone has any idea for this problem I would be grateful. P.S.: I am thinking to remove the loop although it just fails in the first loop pass.
(I get this in the output when the program fails)

Expected data size: 10
Repeat: 0 ...
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid3793.hprof ...
Heap dump file created [1404020 bytes in 0.015 secs]
Exception in thread "1" java.lang.OutOfMemoryError: Java heap space
    at planetlab.app.factory.Worker$15.run(Worker.java:796)
    at java.lang.Thread.run(Thread.java:662)

Something irrelevant:
What's with the code not properly formatted/intended error when making posts in stack overflow? It took me 15 minutes to figure out what to do :@ :S :@

As the title suggests I'm getting this error inside a thread.

The offending LOCs looks like this:

for (int i = 0; i < objectListSize; i++) {
   logger.INFO("Loop repeat: "+i+" ...", true);
   final Double discreteScore = sp.getDouble(superPeerSocket);
   final int expectedObjectIDs = sp.getInteger(superPeerSocket);
   final String discreteObjects[] = new String[expectedObjectIDs];
   for ( int j = 0; j < expectedObjectIDs; j++)
      discreteObjects[j] = sp.getString(superPeerSocket);
   htPlus.attachInitialDiscreteList2L1(discreteScore, discreteObjects); 
}


The final String discreteObjects[] declaration is where I get the error. I am running this code inside a thread. I have two threads currently active when I get this. I also tried using the MAT tool from eclipse. here is a link with some chart files inside:
PLC chart files (dropbox URL)
If anyone has any idea for this problem I would be grateful.
P.S.: I am thinking to remove the loop although it just fails in the first loop pass.
(I get this in the output when the program fails)

Expected data size: 10
Repeat: 0 ...
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid3793.hprof ...
Heap dump file created [1404020 bytes in 0.015 secs]
Exception in thread "1" java.lang.OutOfMemoryError: Java heap space
    at planetlab.app.factory.Worker$15.run(Worker.java:796)
    at java.lang.Thread.run(Thread.java:662)


Something irrelevant:
What's with the code not properly formatted/intended error when making posts in stack overflow? It took me 15 minutes to figure out what to do :@ :S :@

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

风向决定发型 2024-12-09 02:13:15

每个 Java 程序都运行在沙箱中。虽然您的操作系统可能有 10 GB 可用 RAM,但您的应用程序可能只有 128 MB。

您需要使用 -Xms -Xmx 参数确保您的应用程序有足够的内存分配给 JVM。 -Xms 表示最小值,-Xmx 表示最大值

评论中建议您的预期ObjectID 似乎有点高。我当然会检查一下。但是,您可以使用以下代码来了解内存使用情况和可用内存。使用该信息,您可以相应地调整您的 -Xms -Xmx 。

祝你好运!

Runtime runtime = Runtime.getRuntime();  

long maxMemory = runtime.maxMemory();  
long allocatedMemory = runtime.totalMemory();  
long freeMemory = runtime.freeMemory();  

System.out.println("free memory: " + freeMemory / 1024);  
System.out.println("allocated memory: " + allocatedMemory / 1024);  
System.out.println("max memory: " + maxMemory /1024);  
System.out.println("total free memory: " +   
   (freeMemory + (maxMemory - allocatedMemory)) / 1024);   

Every Java program runs in a sandbox. While your OS might have 10 GB of RAM available to it your app might only have 128 MB.

You need to make sure you app has enough ram allocated to the JVM by using the -Xms -Xmx arguments. -Xms indicates the minimum and -Xmx the maximum

It's been suggested in the comments that your expectedObjectIDs seem kinda high. I would certainly check that out. However, you can use the following code to get an idea as you to memory usage and available memory. Using that info you can adjust your -Xms -Xmx accordingly.

Good luck!

Runtime runtime = Runtime.getRuntime();  

long maxMemory = runtime.maxMemory();  
long allocatedMemory = runtime.totalMemory();  
long freeMemory = runtime.freeMemory();  

System.out.println("free memory: " + freeMemory / 1024);  
System.out.println("allocated memory: " + allocatedMemory / 1024);  
System.out.println("max memory: " + maxMemory /1024);  
System.out.println("total free memory: " +   
   (freeMemory + (maxMemory - allocatedMemory)) / 1024);   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文