如何在不增加JVM的情况下处理堆空间?

时间:2012-05-16 14:03:06

标签: java heap-memory

如下修改MP3文件导致Out of Memory错误。无论如何,我可以更有效地进行以下操作(即使用较少的内存)

public void BacaMP3(){
    String a = System.getProperty("user.dir") + "/src/MP3/21.waltz-cut.mp3";
 String bitMP3="";
    try {
         File song = new File(a);
         FileInputStream file = new FileInputStream(song);

         int  input = 0;
         System.out.println("Creating file ...");
         while (input != -1) {
             input = file.read();
             count++;
             if (input==-1)bitMP3="#";
             else{
                 bitMP3 = Integer.toBinaryString(input);
                 while(bitMP3.length()<8){
                    bitMP3="0"+bitMP3;
                 }
             }

             area1.append(bitMP3+"\n");

         }
         System.out.println(count);
         file.close();

         System.out.println("Done");
     } catch (Exception e) {
         System.out.println("Error  " + e.toString());
     }
}

1 个答案:

答案 0 :(得分:0)

作为首发

while(bitMP3.length()<8){
    bitMP3="0"+bitMP3; // Here you are creating two string till count is less than 8 move it to string buffer
}

area1.append(bitMP3+"\n"); // Here you are already using string buffer why doing string concatenation then change to area1.append(bitMP3).append("\n");