1

Im reading image files off an sdcard of an android device. This results in an out of memory fatal error due to a memory leak. I have narrowed it down to an allocation of 1 byte arrays that are not being removed by the GC. I use FileInputStream to read in the file is there a more efficient way to do this? Can you see the cause of the memory leak? Thanks

    private String getHexFileString(File _file)
    {
        byte[] byteStream = new byte[(int) _file.length()];
        String fileHexString = null;
        try
        {
            FileInputStream fis = new FileInputStream(_file);
            fis.read(byteStream);
            fis.close();
            fis = null;
            fileHexString = byteArrayToHexString(byteStream);
        }
        catch (FileNotFoundException e1) 
        {   actLog.addMessage(new ErrorMessage(e1));    }
        catch (IOException e2) 
        {   actLog.addMessage(new ErrorMessage(e2));    }
        catch(OutOfMemoryError e3)
        {   actLog.addMessage(new ErrorMessage(e3));    }


        return fileHexString;
    }

/**
 * This method formats a byte-array into a hex string
 * 
 * @param b byte-array
 * @return hex string
 */
     public String byteArrayToHexString(byte[] b) 
     {
        char[] hexVal = new char[b.length * 2];
        int value = 0;
        for (int i = 0; i < b.length; i++) 
        {
             value = (b[i] + 256) % 256;
             hexVal[i * 2 + 0] = kDigits[value >> 4];
             hexVal[i * 2 + 1] = kDigits[value & 0x0f];
        }
        return new String(hexVal);
     }        
3
  • 1
    How big is each image? And does the return value of getHexFileString() get cached anywhere? Commented Jul 8, 2011 at 11:58
  • Also, why do you need the hex value of the bytes which represent the image? Commented Jul 8, 2011 at 12:01
  • Another question to the long list of questions :-) Do you get the out of memory if you don't call the function byteArrayToHexString() ? Commented Jul 8, 2011 at 12:07

2 Answers 2

2

If some exception occure while you read you will not close the file. It's better to use block finally

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

1 Comment

++ to that. That could be where you lose some resources too. When opening a stream (either input or output) make sure you have a finally block and always close in that block. Then you are assured it will be closed.
2

You are likely to find that converting to a hex String is far more expensive in time and memory than reading the file. i.e. the String will be 4x bigger.

If you want to improve efficiency and avoid running out of memory you should use a method which returns a byte[] and you operate on this array.

4 Comments

Sorry for the delay. The image range from 500 kb up to 1.5mb, I need the value in hex of bytes as I am sending the image to a server. my encryption works off the hex value. I have not tried to exclude the call to byteArrayToHexString() but I believe this is were the memory leak is occurring. Peter- could you elaborate a little more on using another method? Thanks
To reduce the memory you can read and encode the data progressively. This would cut your consumption to a few KB regardless of the size of the file. To have a memory leak you have to hold onto a reference to memory and you don't have one.
The problem is I have alot of steps to perform on the data before I encode it. I need to have a the hex value stored before encoding. When I run the application It retrieve a group of image files and sends them one by one. I will send a certain number of image every time before running out of memory. This leads me to believe that I am not removing used memory from the heap. I have narrowed it down to the byteArrayToHexString() method but I can't see the memory leak.
If there is a memory leak, it is not in this method. It could be that the String created here is being retained elsewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.