Savio menezes
Savio menezes

Reputation: 140

getting outOfMemoryError: Java Heap Space while reading file in java

I have written a program to get data from the file. Whenever I am trying to find data from small files my program works properly but error occurs when file is large. I tried to increase heap size in eclipse but nothing waorks.Below is my code.

 public static String getHiddenDataFromImage(String imagePath)
      {
        String dataContents = null;
        try
        {
          File file = new File(imagePath);
          byte[] fileData = new byte[(int)file.length()];
          InputStream inStream = new FileInputStream(file);
          inStream.read(fileData);
          inStream.close();
          String tempFileData = new String(fileData);
          String finalData = tempFileData.substring(tempFileData.indexOf(extraStr) + extraStr.length(), tempFileData.length());
          byte[] bytefinalData=finalData.getBytes();
          byte[] temp = (byte[]) new Base64().decode(bytefinalData);
          dataContents = new String(temp);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }

        return dataContents;
      }


     public static void main(String[] args){
    System.out.println(ImageAnalyzerUtil.getHiddenDataFromImage2("C:/example.m4b"));
  }

Output

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at ImageAnalyzerUtil.getHiddenDataFromImage(ImageAnalyzerUtil.java:199)
    at ImageAnalyzerUtil.main(ImageAnalyzerUtil.java:332)

line 199 is byte[] fileData = new byte[(int)file.length()];

Upvotes: 2

Views: 1636

Answers (3)

Sandeep Mittal
Sandeep Mittal

Reputation: 57

As suggested by @Maurice and @EJP, avoid reading the whole file at once in the program. But if you have to read the whole file, you can specify runtime parameters to java to give you a larger heap size as follows.

-Xms - Set initial Java heap size -Xmx - Set maximum Java heap size

java -Xms512m -Xmx1024m JavaApp

I am not familiar with Eclipse so I don't know how to increase the heap size from within the IDE. But you can test your program on command line using 'java' programs as mentioned above to see if your program works with larger heap size.

Upvotes: -1

user207421
user207421

Reputation: 311039

  1. Don't read entire files into memory. At some point they won't fit.
  2. Don't use String as a container for binary data.

All you need to do here is seek() to the required offset, read that many bytes, Base64-decode them, and return them.

Upvotes: 2

Maurice Perry
Maurice Perry

Reputation: 32831

It seems that your program is doing a base 64 decoding of a file. It doesn't need to read the whole file in memory at once: there are libraries that can decode an input stream into an output stream.

Upvotes: 1

Related Questions