pdfrx icon indicating copy to clipboard operation
pdfrx copied to clipboard

Memory not clear when pop from screen

Open FeofanGreek opened this issue 1 year ago • 6 comments

Hello. We noticed an unpleasant feature for iOS. After loading any PDF file using a controller in the widget, the memory is not cleared when leaving the screen. We collect more then 200 MBytes of garbage I wanted to add a dispose controller or a cancel controller in dispose block of StateFullWidget, but the controller does not have such a methods..

FeofanGreek avatar Jun 21 '24 10:06 FeofanGreek

Could you please explain your issue in detail?

After loading any PDF file using a controller in the widget

I cannot understand the word "controller" in the sentence. At least, PdfViewerController does not have mechanism to load PDF files.

espresso3389 avatar Jun 21 '24 17:06 espresso3389

I mean PdfViewerController, but I may be wrong in describing the mechanism of its operation. I just assumed that it should be responsible for clearing memory after the user leaves the screen with the PDF file downloaded and displayed. I have attached 3 screenshots from our application to the message, as an example of showing memory consumption. before loading before loading

when loading when loading

after disposing after disposing

As you can see, the increase in memory consumption of 300 megabytes was not cleared after the screen displaying the PDF was closed

FeofanGreek avatar Jun 22 '24 04:06 FeofanGreek

I tested pdfrx with the following code and it reveals that only the first time load caches "something" on memory but repeatedly open/close the PDF page does not consume more. I think the document itself seems correctly closed (Actually, it's opened and closed everytime use some Pdfium functions).

The problem is with some resource caches by Pdfium. The possible cached resources are fonts and color profiles though I don't know the actual implementation.

import 'package:flutter/material.dart';
import 'package:pdfrx/pdfrx.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Test'),
      ),
      body: Center(
          child: TextButton(
        onPressed: () {
          Navigator.of(context)
              .push(MaterialPageRoute(builder: (context) => const PdfPage()));
        },
        child: const Text('Press me!'),
      )),
    );
  }
}

class PdfPage extends StatefulWidget {
  const PdfPage({super.key});

  @override
  State<PdfPage> createState() => _PdfPageState();
}

class _PdfPageState extends State<PdfPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pdf Page'),
      ),
      body: PdfViewer.uri(
        Uri.parse(
            'https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf'),
      ),
    );
  }
}

espresso3389 avatar Jun 24 '24 13:06 espresso3389

It's a shame, well, let's live with it for now and hope that it will correct itself

FeofanGreek avatar Jun 25 '24 04:06 FeofanGreek

@FeofanGreek 696527b introduces chromium/6555 and the memory consumption by the pdfium cache seems reduced. Could you please try it?

espresso3389 avatar Jun 26 '24 17:06 espresso3389

unfortunately it hasn't changed

FeofanGreek avatar Jun 27 '24 16:06 FeofanGreek

As explained on #430, this behavior is by design and not a bug.

espresso3389 avatar Oct 16 '25 16:10 espresso3389