Java上的堆内存问题

问题描述 投票:0回答:1

我正在尝试运行以下程序,但在StringBuilder附加循环结构期间收到OutOfMemory错误。

  1. 我正在尝试采取任何措施来降低内存使用量,使其足以读取CSV文件(超过200,000行,但只有3列:item,rating,user)。
  2. 然后我将创建一个二维int数组,其中唯一的项代表行,唯一用户代表列,而交集为评分。
  3. 最后,我将使用StringBuilder帮助创建输出CSV文件

感谢您的帮助和时间。

      List<String> userList = new ArrayList<String>();
      List<String> itemList = new ArrayList<String>();


      FileInputStream stream = null;
      Scanner scanner = null;
      int[][] layout = new int[10672][24303];

      int indexItemList = 0;
      double temp = 0;

      try{
         stream = new FileInputStream(fileName);
         scanner = new Scanner(stream, "UTF-8");
         while (scanner.hasNextLine()){
             String line = scanner.nextLine();
             if (!line.equals("")){
                String[] elems = line.split(",");
                if (indexItemList == 0) {
                    temp = Double.valueOf(elems[1]);
                  layout[0][0] = (int)temp;
                    itemList.add(elems[0]);
                    userList.add(elems[2]);
                    indexItemList++;
                }
                else {
                    boolean itemFound = itemList.contains(elems[0]);
                    boolean userFound = userList.contains(elems[2]);

                    int indexItem = 1;
                    int indexUser = 1;
                    if ((itemFound) && (userFound)) {
                        indexItem = itemList.indexOf(elems[0]);
                        indexUser = userList.indexOf(elems[2]);
                     temp = Double.valueOf(elems[1]);
                        layout[indexItem][indexUser] = (int)temp;
                    }                    
                    else if ((itemFound) && (!userFound)) {
                        userList.add(elems[2]);
                        indexItem = itemList.indexOf(elems[0]);
                        indexUser = userList.indexOf(elems[2]);
                     temp = Double.valueOf(elems[1]);
                        layout[indexItem][indexUser] = (int)temp;
                    }
                    else if ((!itemFound) && (userFound)){
                        itemList.clear();
                        itemList.add(elems[0]);
                        indexUser = userList.indexOf(elems[2]);
                     temp = Double.valueOf(elems[1]);
                        layout[indexItemList][indexUser] = (int)temp;
                        indexItemList++;
                    }
                    else if (!((itemFound) && (userFound))) {
                        itemList.clear();
                        itemList.add(elems[0]);
                        userList.add(elems[2]);
                        indexUser = userList.indexOf(elems[2]);
                     temp = Double.valueOf(elems[1]);
                        layout[indexItem][indexUser] = (int)temp;
                        indexItemList++;
                    }   
                }
             }
         } 
         if (scanner.ioException() != null){
            throw scanner.ioException();
         }
      }
      catch (IOException e){
         System.out.println(e);
      }
      finally{
         try{
            if (stream != null){
               stream.close();
            }
         }
         catch (IOException e){
            System.out.println(e);
         }
         if (scanner != null){
            scanner.close();
         }
      }

      StringBuilder sb = new StringBuilder();

      for (int i = 0; i < layout.length; i++){
          for (int j = 0; j < layout[i].length; j++){
             sb.append(layout[i][j] + "");
             layout[i][j] = 0;
             if (j < layout[i].length - 1){
                sb.append(",");
             }
          }
          sb.append("\n");
       }
java csv memory-management out-of-memory heap-memory
1个回答
2
投票

您的文件有200,000行,但是您的2D数组具有259'361'616单元,StringBuilder的大小将与该数字成比例。您无需存储所有这些信息:这是一个非常空心的矩阵。

© www.soinside.com 2019 - 2024. All rights reserved.