Baseflow / Baseflow/flutter_cached_network_image
Memory leak on real device only
- Dominant language
- Dart
- Stars
- 2.6k
- Forks
- 731
- Avg merge
- 3d 9h
- Merged PRs (30d)
- 1
Description
## 🐛 Bug Report
On real device Android & iOS this package have a memory leak.
Our app got some crash in production because of this issue : we have a long list of product inside a paginated infinite list. User can scroll on it and some of them reported crash. After investigation we discover this memory leak.
### Expected behavior
Constant RSS usage
### Reproduction steps
Use code bellow and check on devtools the memory usage graph.
With CachedNetworkImage : (increase during scroll)

With Image.network: (stable during scroll)

```dart
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.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: false,
),
home: const Home(),
);
}
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State createState() => _HomeState();
}
class _HomeState extends State {
bool useCachedNetwork = true;
@override
Widget build(BuildContext context) {
return Builder(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Memory leak"),
),
floatingActionButton: FloatingActionButton.extended(
label: Text(
"Use ${useCachedNetwork ? "Image.network" : "CachedNetworkImage"}",
),
onPressed: () {
setState(() => useCachedNetwork = !useCachedNetwork);
},
),
body: ListView.builder(
itemBuilder: (BuildContext context, int index) => SizedBox(
height: 80,
child: Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
AspectRatio(
aspectRatio: 1,
child: useCachedNetwork
? CachedNetworkImage(
imageUrl: "https://picsum.photos/id/$index/1000/1000",
errorListener: (_) {},
progressIndicatorBuilder: (
BuildContext context,
String url,
DownloadProgress progress,
) =>
Center(
child: CircularProgressIndicator(
value: progress.progress,
),
),
errorWidget: (_, __, ___) => const Center(
child: Icon(Icons.error),
),
)
: Image.network(
"https://picsum.photos/id/$index/1000/1000",
errorBuilder: (_, __, ___) => const Center(
child: Icon(Icons.error),
),
),
),
const SizedBox(width: 16),
Expanded(
child: Text(
index.toString(),
),
),
],
),
),
),
),
),
);
},
);
}
}
```
### Configuration
**Versions:**
```yaml
flutter: 3.16.8
cached_network_image: 3.3.1
```
**Platform:**
- [x] :iphone: iOS
- [X] :robot: Android (Pixel 7 Android 14)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.