Java填充堆空间之前崩溃 [英] Java crashing before filling up heap space

查看:112
本文介绍了Java填充堆空间之前崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个需要处理大量数据的java模拟器。它工作正常,但我得到一个 int [100000] [100] [2] 的数组以及其他大数组。该程序说它内存不足。 ( Java.lang.outOfMemoryError

I have this java simulator that will need to handle a huge amount of data. It works fine, but one I get up to an array of int[100000][100][2] along with other big arrays. the program says that its out of memory. (Java.lang.outOfMemoryError)

一切都很好,我只是给它更多的记忆,但似乎即使我允许它2GB,总是耗尽~300M左右。这一切都来自观看任务经理。

All fine and good, I just give it more memory, but It seems to always run out around ~300M even though I allow it 2GB. This is all from watching Task manager.

我的系统出了什么问题,或者这只是我需要处理的java事情?

Is there something wrong with my system, or is this just a java thing that I need to deal with?

@DanielPryden

@DanielPryden

OS:Win 7 32Bit 4GB ram

OS: Win 7 32Bit 4GB of ram on board

JVM命令:java -Xmx2048M -Xms2048M模拟器

JVM Command: java -Xmx2048M -Xms2048M Simulator

错误数据:必须从IDE(使用IntelliJ)获取。我不知道如何从cmd做到这一点。我认为这正是你要找的。

Error Data: Had to get from an IDE (using IntelliJ). I dont know how to do it from cmd. I assume this is what you are looking for.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Simulator.main(Simulator.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


推荐答案

如果您在32位Windows操作系统上运行,则无法分配完整的2GB。即使你搞砸了Windows内部,你将获得的最大可用地址空间是3GB,即使这样,它也不会是连续的(并且JVM需要连续的空间来构建Java堆)。实际上,使用Sun / Oracle JVM,我从来没有能够成功地分配任何大于1.5GB的堆 - 如果您正在使用JNI,那么您链接的任何DLL都会减少最大可能堆in。

If you're running on a 32-bit Windows OS, it's going to be impossible to allocate a full 2GB. Even if you muck about with Windows internals, the largest usable address space you'll ever get is 3GB, and even then it won't all be contiguous (and the JVM requires contiguous space to build the Java heap). In practice, with the Sun/Oracle JVM, I've never been able to successfully allocate a heap any bigger than about 1.5GB -- and if you're using JNI at all, the maximum possible heap is reduced by any DLLs you link in.

如果你真的需要一个大堆,我会建议你尽可能转移到64位操作系统。其次,正如其他答案所指出的,如果你可以分配非连续内存,那么更有可能成功。使用 LinkedList 或以单独的块分配数据的其他结构。这里有一点折衷;你可能希望每个块都包含一个至少64Kb的数组。

If you really need a big heap, I would first recommend you move to a 64-bit OS if at all possible. Secondly, as other answers here point out, if you can allocate non-contiguous memory, that will be more likely to succeed. Use a LinkedList or other structure that allocates data in separate chunks. There is a bit of a trade-off here; you probably want each chunk to contain an array that's at least 64Kb.

最后,如果你能找到一种方法将处理拆分为单独的进程,你可能会得到更好的结果 - 即,运行多个Java实例,每个实例都有自己的数据集进行操作,并使用套接字或文件在它们之间进行通信。也就是说,使用32位Windows操作系统,你仍然可能无法使用4GB内存,更不用说任何更大的内存。

Finally, you might get better results if you can find a way to split up your processing into separate processes -- that is, run multiple Java instances, each with its own set of data to operate on, and use sockets or files to communicate between them. That said, with a 32-bit Windows OS, you still probably won't be able to make use of your 4GB of RAM, let alone any larger amount.

这篇关于Java填充堆空间之前崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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