【问题标题】:How to handling heap Space without increase JVM?如何在不增加 JVM 的情况下处理堆空间?
【发布时间】:2012-05-24 02:46:26
【问题描述】:

如下修改 MP3 文件会导致内存不足错误。无论如何我可以更有效地执行以下操作(即使用更少的内存)

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());
     }
}

【问题讨论】:

  • 你到底想做什么?你为什么要这样做?
  • 您的问题需要重新表述,以便我们理解问题。你的意思是你在执行上述程序时内存不足?如果是这样,您正在使用的最小/最大堆是多少以及 mps 文件有多大。在 sidenode 上,您在程序中为 bitMP3 使用 String,请改用 StringBuffer
  • 我想做 MP3 处理,我想更改 MP3 中的一些内容....但是当我这样做时....我的内存快用完了..我尝试增加 JVM 但它的还是不够。
  • 按照评论中的建议使用 StringBuffer 或 StringBuilder 运行它。您正在使用的堆大小是多少,您正在处理的文件大小是多少
  • readMp3方法是第一步,下一步是处理MP3位。当我运行下一步时,我使用 1500M 内存和 6MB MP3 大小...

标签: java heap-memory


【解决方案1】:

作为首发

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");

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    相关资源
    最近更新 更多