简体   繁体   English

Java中的大数组导致的内存不足异常

[英]out of memory exception caused by big array in java

I'm implenmenting an algorithm which based on probabilistic latent semantic indexing(plsa) and the paper is here and it need a four dimension array which named p_z_d_wt_wv, z is topic, d is document, wt is text word, wv is visual word,and the number of each dimension is about 12, 7000,100, 500, and the array is a double array, so it need 32G memory !! 我正在实现一种基于概率潜在语义索引(plsa)的算法,本文在这里 ,它需要一个名为p_z_d_wt_wv的四维数组,z是主题,d是文档,wt是文本词,wv是可视词,每个维数大约为12、7000、100、500,并且该数组是双精度数组,因此需要32G内存 I allocate this memory like this way below, and it is just for demonstration as the number of wt and wv in each document is different. 我以这种方式分配此内存,以下内容仅用于演示,因为每个文档中wt和wv的数量不同。

p_z_d_wt_wv = new double[12][7000][][]; 
for( int t = 0; t < 12; ++t) 
{ 
    for( int d = 0; d < 7000; ++d ) 
    { 
        p_z_d_wt_wv[t][d] = new double[100][500];
    } 
}

when I run the code, it has out of memory problem. 当我运行代码时,它有内存不足的问题。 First, why do my code run out of memory? 首先,为什么我的代码用完了内存? Are the memory allocated consecutively if the array are allocated in my way? 如果以我的方式分配数组,是否连续分配内存? Is it because java have a memory limit for consecutive memory? 是因为Java对连续内存有内存限制吗? If so, what's the limit? 如果是这样,有什么限制?

Second, what can I do to solve this problem supposed that the memory of the server is big enough. 其次,假设服务器的内存足够大,我该怎么办才能解决此问题。 I know I can change it as a float array, but are there any other solutions? 我知道我可以将其更改为float数组,但是还有其他解决方案吗?

If you actually need all of that memory, well, you need all of that memory. 如果您确实需要所有这些内存,那么您就需要所有这些内存。

There are some alternatives: 有一些替代方法:

  1. You could look into using memory mapped files. 您可以考虑使用内存映射文件。

  2. If the array has a lot of zeros in it, you could store it as a sparse matrix representation (don't explicitly store 0s). 如果数组中有很多零,则可以将其存储为稀疏矩阵表示形式(不要显式存储0)。

  3. If you don't need the whole thing in memory at once, you could also store it in some sort of persistent storage (file, database, etc) and only access the parts you need at any given time. 如果您不需要一次将整个内容存储在内存中,也可以将其存储在某种持久性存储中(文件,数据库等),并且仅在任何给定时间访问所需的部分。

Are the memory allocated consecutively if the array are allocated in my way? 如果以我的方式分配数组,是否连续分配内存? Is it because java have a memory limit for consecutive memory? 是因为Java对连续内存有内存限制吗? If so, what's the limit? 如果是这样,有什么限制?

No, the JVM can not allocate memory for your array. 不,JVM无法为您的阵列分配内存。 if you use float for your array, you must set the maximum memory heap space 16GB. 如果对数组使用float,则必须将最大内存堆空间设置为16GB。 You can use file to store your array. 您可以使用文件存储阵列。

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

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