2

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

2
  • 2
    How exactly did you try "to increase heap size in eclipse"? And how large can your files be? Commented Mar 24, 2015 at 6:47
  • my project is in maven so i added in pom. My file size is 140MB Commented Mar 24, 2015 at 6:55

3 Answers 3

2
  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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

-1

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.

1 Comment

He doesn't have to read the whole file. You never have to read the whole file: the only exception I've ever been able to think of is images, when and only when you're going to display them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.