简体   繁体   English

将2D矩阵存储在CSV中:java.lang.OutOfMemoryError

[英]Store 2d matrix in CSV: java.lang.OutOfMemoryError

I wrote the following function for calculating cost matrix and storing it in CSV file: 我编写了以下函数来计算成本矩阵并将其存储在CSV文件中:

private static void calculateCostMatrix()
{
    int len = _POIs.size();
    CostMatrix = new double[len][len];
    for (int i=0; i<len; i++)
    {
        int ic = _POIs.get(i).getId();
        for (int j=i; j<len; j++)
        {
            int jc = _POIs.get(j).getId();
            double dist = euclideandist(_POIs.get(i).getLat(),_POIs.get(i).getLon(),
                                    _POIs.get(j).getLat(),_POIs.get(j).getLon());
            CostMatrix[ic][jc] = dist;
            CostMatrix[jc][ic] = dist;
        }           
    }

    // Save in CSV
    try
    {
        String NEW_LINE = System.getProperty("line.separator");
        File file = new File("CostMatrix.csv");
        FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
        BufferedWriter bw = new BufferedWriter(fw);
        StringBuilder sb = new StringBuilder();;
        for (double[] row : CostMatrix) 
        {
            for (double d : row)
            {
                 sb.append(d);
                 sb.append(",");
            }
            sb.append(NEW_LINE);
        }
        bw.write(sb.toString());
        bw.close();
        }

        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

However, obviously, due to large number of elements (200,000 x 200,000) I get the message java.lang.OutOfMemoryError: Java heap space . 但是,显然,由于大量元素(200,000 x 200,000),我收到消息java.lang.OutOfMemoryError: Java heap space

So, instead of pre-allocating a space for CostMAtrix, I now want to directly calculate and store each cell in CSV file. 因此,我现在不想直接为CostMAtrix分配空间,而是直接计算每个单元格并将其存储在CSV文件中。 How can I do this in the proper way? 我该如何以正确的方式做到这一点?

I would do it differently. 我会做不同的事情。 Since the problem is memory, you could use a temporary binary file on disk instead of an array to store the result. 由于问题是内存,因此可以在磁盘上使用临时二进制文件而不是数组来存储结果。 Create this temporary files of the right size, then seek through it to store the double results (in binary form). 创建此大小合适的临时文件,然后查找以存储重复结果(二进制格式)。

Once you are done processing the whole matrix in binary, parse the file to convert it to CSV. 一旦您完成了对整个矩阵的二进制处理,请解析该文件以将其转换为CSV。

Otherwise, you'll have to sort your POIs by value and keep their index as a reference for the CSV row/column loop. 否则,您将必须按值对POI进行排序,并保留它们的索引作为CSV行/列循环的参考。

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

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