如何处理“java.lang.OutOfMemoryError:Java堆空间”错误? [英] How to deal with "java.lang.OutOfMemoryError: Java heap space" error?

查看:99
本文介绍了如何处理“java.lang.OutOfMemoryError:Java堆空间”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Java 5 上编写客户端 Swing 应用程序(图形字体设计器)。最近,我遇到了 java.lang.OutOfMemoryError:Java堆空间错误,因为我对内存使用情况并不保守。用户可以打开无限数量的文件,程序将打开的对象保存在内存中。经过快速研究后,我发现 5.0 Java虚拟机中的人机工程学和其他人在Windows机器上说JVM默认最大堆大小为 64MB

I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage. The user can open unlimited number of files, and the program keeps the opened objects in the memory. After a quick research I found Ergonomics in the 5.0 Java Virtual Machine and others saying on Windows machine the JVM defaults max heap size as 64MB.

鉴于这种情况,我应该如何处理这个约束?

Given this situation, how should I deal with this constraint?

我可以使用命令行选项增加最大堆大小,但是需要找出可用的RAM并编写一些启动程序或脚本。此外,增加到某个有限的最大值并不会最终摆脱这个问题。

I could increase the max heap size using command line option to java, but that would require figuring out available RAM and writing some launching program or script. Besides, increasing to some finite max does not ultimately get rid of the issue.

我可以重写一些代码来经常将对象持久化到文件系统(使用数据库是同样的事情)释放记忆。它可以工作,但也可能有很多工作。

I could rewrite some of my code to persist objects to file system frequently (using database is the same thing) to free up the memory. It could work, but it's probably a lot work too.

如果你能指出我上面的想法的细节或一些替代方案,如自动虚拟内存,扩展堆动态大小,这将是伟大的。

If you could point me to details of above ideas or some alternatives like automatic virtual memory, extending heap size dynamically, that will be great.

推荐答案

最终你总是有一个有限的最大堆使用无论如何你正在运行什么平台。在Windows 32位中,这大约是2gb(不是专门的堆,而是每个进程的总内存量)。只是碰巧Java选择使默认值更小(可能是因为程序员无法创建具有失控内存分配的程序而不会遇到这个问题并且必须仔细检查他们正在做什么)。

Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2gb (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).

因此,您可以采用多种方法来确定所需的内存量或减少所使用的内存量。垃圾收集语言(如Java或C#)的一个常见错误是保留对不再使用的对象的引用,或者在可以重用它们时分配许多对象。只要对象具有对它们的引用,它们将继续使用堆空间,因为垃圾收集器不会删除它们。

So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.

在这种情况下,您可以使用Java内存分析器确定程序中的哪些方法正在分配大量对象,然后确定是否有办法确保它们不再被引用,或者首先不分配它们。我过去使用的一个选项是JMP http://www.khelekore.org/jmp/

In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.

如果您确定要分配这些对象是出于某种原因而需要保留引用(取决于您正在执行的操作,可能就是这种情况) ),您只需要在启动程序时增加最大堆大小。但是,一旦进行了内存分析并了解了如何分配对象,您应该更好地了解所需的内存量。

If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.

一般情况下,如果你不能保证你的程序会在一些有限的内存中运行(可能取决于输入的大小),你总会遇到这个问题。只有在耗尽所有这些后才需要查看缓存对象到磁盘等。此时你应该有一个很好的理由说我需要Xgb的内存,你不能通过改进来解决它您的算法或内存分配模式。通常情况下,这通常只适用于在大型数据集(如数据库或某些科学分析程序)上运行的算法,然后缓存和内存映射IO等技术变得有用。

In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.

这篇关于如何处理“java.lang.OutOfMemoryError:Java堆空间”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆