Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

java - 如何处理“ java.lang.OutOfMemoryError:Java堆空间”错误?(How to deal with “java.lang.OutOfMemoryError: Java heap space” error?)

I am writing a client-side Swing application (graphical font designer) on Java 5 .

(我正在Java 5上编写客户端Swing应用程序(图形字体设计器)。)

Recently, I am running into java.lang.OutOfMemoryError: Java heap space error because I am not being conservative on memory usage.

(最近,我遇到了java.lang.OutOfMemoryError: Java heap space错误,因为在内存使用方面我并不保守。)

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 .

(经过快速研究,我在5.0 Java虚拟机中发现了人机工程学,其他人则说在Windows计算机上,JVM的默认最大堆大小为64MB 。)

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

(在这种情况下,我应该如何处理这种限制?)

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.

(我可以使用java的命令行选项来增加最大堆大小 ,但这需要找出可用的RAM并编写一些启动程序或脚本。)

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.

(如果您可以将上述想法的细节或自动虚拟内存,动态扩展堆大小等替代方案的细节介绍给我,那将会很棒。)

  ask by Eugene Yokota translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

(在Windows 32位中,这大约为2GB (不是专门用于堆,而是每个进程的内存总量)。)

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选择减小默认值(大概是为了让程序员在没有遇到这个问题且不必仔细检查它们在做什么的情况下,无法创建内存分配失控的程序)。)

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.

(Java或C#等垃圾收集语言的一个常见错误是,保留对不再使用的对象的引用,或者在可以重用它们时分配许多对象。)

As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.

(只要对象引用了它们,它们将继续使用堆空间,因为垃圾收集器不会删除它们。)

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.

(在这种情况下,您可以使用Java内存探查器来确定程序中哪些方法正在分配大量对象,然后确定是否有办法确保不再引用它们,或者首先不分配它们。)

One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/ .

(我过去使用的一个选项是“ 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.

(但是,一旦执行了内存分析并了解了对象的分配方式,就应该对所需的内存有一个更好的了解。)

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.

(仅在用尽所有这些之后,您才需要研究将对象缓存到磁盘等中。在这一点上,您应该有一个很好的理由说“我需要Xgb的内存”,您不能通过改进来解决它。您的算法或内存分配模式。)

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.

(通常,只有在大型数据集(例如数据库或某些科学分析程序)上运行的算法才会出现这种情况,然后诸如缓存和内存映射IO之类的技术就变得有用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...