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

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

问题描述

我正在Java 5 上编写客户端Swing 应用程序(图形字体设计器).最近,我遇到了 java.lang.OutOfMemoryError: Java heap space 错误,因为我对内存使用并不保守.用户可以打开无限数量的文件,程序将打开的对象保存在内存中.经过快速研究,我发现 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?

我可以使用命令行选项增加最大堆大小到java,但这需要找出可用的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 heap space"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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