PdfiumAndroid icon indicating copy to clipboard operation
PdfiumAndroid copied to clipboard

Memory leak when opening PDF as a byte array

Open ViktorSkabiak opened this issue 3 years ago • 5 comments

The library contains a memory leak when you open a PDF as a byte array. I check the method nativeOpenMemDocument and found some weird memory copying code. I've commented this code and memory leak has gone image

TL;DR I was running a 3 minutes autotest which opens the 50-pages PDF every 10 seconds. This is the memory profiler results before the code commenting: Screenshot 2022-05-16 at 06 23 49 and you can clearly see that native memory grows and GC cannot clear it

This is the memory profiler results after the code commenting: Screenshot 2022-05-16 at 06 29 52 Now there is not such native memory issues. It stays relatively small and GC easily clears it

The issue was happening when I was calling the method PdfDocument newDocument(byte[] data, String password)

ViktorSkabiak avatar May 16 '22 03:05 ViktorSkabiak

@barteksc Please share your thoughts on this issue? Please let me know whether it's fine that I've just commented these lines? Or is there some purpose for such memory copying? Glad to hear your comments. Thanks

ViktorSkabiak avatar May 16 '22 03:05 ViktorSkabiak

UPDATE. After several tests I made a conclusion that the approach describe above is not acceptable. It works in some scenarios but also crashes in others (I cannot describe the details here because of the NDA)

After some research, I created a new code version that works in all the scenarios. These are the changes that I made: image image

The whole code is here: mainJNILib.txt

@barteksc Glad to hear your opinion Thanks

ViktorSkabiak avatar May 16 '22 23:05 ViktorSkabiak

@ViktorSkabiak You should create a pull request to fix this issue.

AhmerAfzal1 avatar May 18 '22 20:05 AhmerAfzal1

@AhmerAfzal1 Should I also include in the pull request the other changes required for the NDK to be able to build it with the latest Android Studio version? Such changes are:

  1. APP_PLATFORM = android-19 // modern NDK doesn't support the android-14 which is currently specified in the code
  2. removed mips and mips64 support // the same reason as above

ViktorSkabiak avatar May 18 '22 23:05 ViktorSkabiak

@AhmerAfzal1 Should I also include in the pull request the other changes required for the NDK to be able to build it with the latest Android Studio version? Such changes are:

  1. APP_PLATFORM = android-19 // modern NDK doesn't support the android-14 which is currently specified in the code
  2. removed mips and mips64 support // the same reason as above

It's best to create a pull request that includes your latest change to this repo. Make it easy for authors and other users to see the new changing and fixed bugs, always make a pull request in which you participate improves this great library.

AhmerAfzal1 avatar May 19 '22 19:05 AhmerAfzal1