简体   繁体   English

Java OutOfMemory堆空间

[英]Java OutOfMemory Heap Space

everyone. 大家。 I am working on a project. 我正在做一个项目。 Along the way, I got the "OutOfMemory" problem with this line of code... 一路上,我得到了这行代码的“OutOfMemory”问题......

this.A = new int[n][n];

...for n = 10000; ......对于n = 10000;

I tried fixing it by adding -Xmx2048m command but the thing is when added, all of the rest of running programs stops responding. 我尝试通过添加-Xmx2048m命令来修复它,但事情是在添加时,所有其余正在运行的程序都停止响应。 Any other suggestion for my case? 对我的案子还有其他建议吗? Thanks a lot 非常感谢

You have to calculate the space that array needs. 您必须计算数组所需的空间。 If it is over 2048M then you'll receive an OutOfMemoryError . 如果它超过2048M,那么你将收到一个OutOfMemoryError

In your case you try to allocate an array of 10000 x 10000 which is 100.000.000 . 在您的情况下,您尝试分配10000 x 10000的数组,即100.000.000

A primitive int occupies 4 bytes. 原始int占用4个字节。 This means that the whole array takes 这意味着整个数组都需要

100.000.000 * 4 bytes which is 0,37 GB of space. 100.000.000 * 4字节,即0,37 GB的空间。

So it seems that there is something else in your program which caueses the error. 因此,您的程序中似乎还有其他事项可以预测错误。 For example if you try to allocate multiple arrays in a loop then you can run out of memory real quick. 例如,如果您尝试在循环中分配多个数组,那么您可以快速耗尽内存。

It can be a problem if your hardware does not have 2048M of memory. 如果您的硬件没有2048M的内存,则可能会出现问题。

It is also possible that before using -Xmx2048m you had for example -Xmx512m which might be too small for your array. 在使用-Xmx2048m之前,您可能还有例如-Xmx512m ,这可能对您的阵列来说太小了。

Use this: 用这个:

Increase heap size in Java 增加Java中的堆大小

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size

java -Xms16m -Xmx64m ClassName java -Xms16m -Xmx64m ClassName

Let's see... 让我们来看看...

An int occupies 32bit or 4 bytes in memory. int在内存中占用32位或4个字节。
Your 2-dimensional array thus requires 10000 * 10000 * 4 bytes = 400000000 bytes = 381.47 MB (plus storage for the array objects) 因此,您的二维数组需要10000 * 10000 * 4字节= 400000000字节= 381.47 MB (加上数组对象的存储空间)

Let's verify that with a simple test: 让我们通过一个简单的测试验证:

int n = 10000;
int[][] foo = new int[n][n];
System.out.println("USED MEMORY: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024);

Result: 384 MB (on my machine, your results may vary). 结果:384 MB(在我的机器上,您的结果可能会有所不同)。

Running out of memory in the heap happens when a requested block of memory cannot be allocated, not necessarily when the memory is full . 当请求的内存块无法分配时,堆中的内存不足, 而不一定是在内存已满时

As you allocate memory in blocks of 40'000 bytes, you can run out of memory when no contiguous free memory of that size is available (hundreds of blocks with less that 40'000 won't help here). 当您以40'000字节的块分配内存时,如果没有可用的连续空闲内存 ,则可能会耗尽内存(数百个小于40'000的块在此处无效)。 That means you can easily run out of memory when the free memory on the heap is fragmented as small memory blocks . 这意味着当堆上的可用内存碎片为小内存块时,您可以轻松耗尽内存

If you happen to allocate that memory multiple times in a row, try reusing the allocated memory instead of allocating it again (to prevent fragmentation to a certain degree). 如果您碰巧连续多次分配该内存,请尝试重用已分配的内存而不是再次分配(以防止碎片到一定程度)。 Alternatively, try using smaller arrays (even if it means using more of them). 或者,尝试使用较小的数组(即使它意味着使用更多的数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM