PdfViewer memory leaks when when widget is unmounted during rendering #226
Replies: 4 comments 2 replies
-
@jezell Do I miss something important to see the actual memory leaks? import 'dart:math';
import 'package:flutter/material.dart';
import 'package:pdfrx/pdfrx.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const PdfrxAsyncDispose(),
);
}
}
class PdfrxAsyncDispose extends StatelessWidget {
const PdfrxAsyncDispose({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) => Container(
margin: const EdgeInsets.all(1),
height: 320,
child: PdfViewer.asset('assets/PDF32000_2008.pdf',
params: PdfViewerParams(
minScale: 0.0001,
onViewerReady: (document, controller) async {
final random = Random();
while (true) {
if (!controller.isReady) return;
final count = controller.pageCount;
await Future.delayed(const Duration(milliseconds: 300));
if (!controller.isReady) return;
final pageNumber = random.nextInt(count) + 1;
await controller.goToPage(pageNumber: pageNumber);
}
},
),
),
),
),
);
}
} |
Beta Was this translation helpful? Give feedback.
-
I'd guess this should be sufficient for getting it to leak every once and a while. The more viewers you have on the screen, the easier it is to reproduce leaks since it primarily happens when widget is removed from the tree during a render operation. It can be hard to see that it is leaking, but I put together a widget that allows you to track the usage of the canvaskit heap on web a while back: https://github.com/jezell/mem_test/tree/main/lib It can be hard to track whether things are leaking without a counter like this, since there isn't currently browser tool or API call in flutter that will show you when it is leaking. Web will start out using 128 MB of memory in the CanvasKit WASM module. The memory will grow if you start using more than that. The MemoryMonitor widget will print out the current size of the CanvasKit heap, so you see how it grows over time. The sample there also shows how (on web) to completely avoid canvaskit heap usage by using a new API that I added to Flutter, createImageFromTextureSource. In the next release, I believe that we'll switch decodeImageFromPixels to use this API by default, but nothing prevents individual libraries from using it now to be more safe and reduce memory problems on web. That will make leaks less prone to crashing flutter web apps since it doesn't use any of the canvaskit heap for the image data. |
Beta Was this translation helpful? Give feedback.
-
@jezell You told me that "if you scroll around quickly in the ListView, sometimes it only takes 30 seconds to generate 2 GB worth of cached PDF images with pdfrx viewers and run the browser out of memory and crash flutter due to these bugs and leaks." but I didn't see any such large leaks and could not see your point. Could you give me some solid example code that causes such memory leaks with some sample PDF file? |
Beta Was this translation helpful? Give feedback.
-
@jezell By the way, the value of _canvasKitMemory does not seem to change runtime. Is it the pre-allocated memory size or such? |
Beta Was this translation helpful? Give feedback.
-
The discussion is for PR #220 that insists potential memory leaks when unmounting PdfViewer widget.
Beta Was this translation helpful? Give feedback.
All reactions