Arc Mods
Arc Mods

Reputation: 21

OutOfMemoryException when reading bytes from file

I'm trying to make a hex dumping application and for that, I need to read the file bytes. I'm using apache io version 2.8.0 to do the hex dumping. This is the code I'm using:

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            try {
                byte[] bytes = Files.readAllBytes(Paths.get(getPackageManager().getApplicationInfo("com.pixel.gun3d", 0).nativeLibraryDir.concat("/libil2cpp.so")));
                Log.e("MainActivity", dumpHex(bytes));
            } catch (PackageManager.NameNotFoundException | IOException e) {
                e.printStackTrace();
            }
        }

    private String dumpHex(byte[] bytes) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            HexDump.dump(bytes, 0, out, 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(out.toByteArray());
    }

And the error I get is this: java.lang.OutOfMemoryError: Failed to allocate a 155189264 byte allocation with 25165824 free bytes and 94MB until OOM, max allowed footprint 328602112, growth limit 402653184 I looked it up and none of the suggestions I tried such as adding android:largeHeap="true" and android:hardwareAccelerated="false" to the manifest worked. Any help is appreciated <3

Upvotes: 0

Views: 534

Answers (1)

Orr Benyamini
Orr Benyamini

Reputation: 405

When reading file it is recommended to partition the read into small chunks to avoid this kind of errors. Reading the whole files bytes into byte array has limitations, for example the file size is over Integer.MAX_VALUE you will get an OutOfMemoryException.

Try doing something similar to this:


byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(file);
int readBytesNum = fis.read(buffer);
while (readBytesNum > 0)
{
    //do what you need
    readBytesNum = in.read(buffer);
}

This means you would need to change the implementation of HexDump to handle files in partitions

Upvotes: 2

Related Questions