Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.OutOfMemoryError #1805

Open
4 tasks
scolnet opened this issue Jan 12, 2025 · 1 comment
Open
4 tasks

java.lang.OutOfMemoryError #1805

scolnet opened this issue Jan 12, 2025 · 1 comment
Labels
bug Something isn't working needs triage

Comments

@scolnet
Copy link

scolnet commented Jan 12, 2025

Describe the bug
Hello @DavBfr,
First of all, thank you for taking the time to develop this flutter library which is very useful.

I occasionally encounter crashes in production due to memory problems when previewing PDFs. I generate photo books, so some PDFs can contain more than 50 pages with lots of photos.

 Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 237479120 byte allocation with 25165824 free bytes and 155MB until OOM, target footprint 264901640, growth limit 402653184
       at java.util.Arrays.copyOf(Arrays.java:3161)
       at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
       at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
       at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:135)
       at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:1)
       at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:1)
       at io.flutter.plugin.common.StandardMethodCodec.encodeMethodCall(StandardMethodCodec.java:17)
       at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:12)
       at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:2)
       at net.nfet.flutter.printing.PrintingHandler.onPageRasterized(PrintingHandler.java:44)
       at net.nfet.flutter.printing.PrintingJob.lambda$rasterPdf$3(PrintingJob.java:7)

To Reproduce
Difficult to reproduce because it depends of the free memory in device

Expected behavior
Be able to generate the rasterization of the pdf without error

Desktop (please complete the following information):

  • iOS
  • [ X] Android
  • Browser
  • Windows
  • Linux

Additional context
I realize that there is no magic solution but perhaps by optimizing the data that passes between the OS and the application (via method channel), we can reduce the consumption of the necessary memory and drastically reduce this type of errors. I am thinking in particular at the transfer of bitmaps which are heavy in bytes.

I understand that for the web, the data that feeds the PdfRaster object is already in PNG format.
Given that the preview widget needs to convert the bytes of the bitmap into PNG, maybe we can take inspiration from the web and pass the PNG image directly in method channel ?

// NOW
 final ByteBuffer buf = ByteBuffer.allocate(stride * height);
                    bitmap.copyPixelsToBuffer(buf);
                    bitmap.recycle();
// using PNG
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
final byte[] b = baos.toByteArray();
baos.close();

I don't know Swift and if it's possible to do the same.

What do you thing about this proposal?

@scolnet scolnet added bug Something isn't working needs triage labels Jan 12, 2025
@Mamena2020
Copy link

Does anyone have a solution? I am experiencing the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants