Baseflow / Baseflow/flutter_cached_network_image

Memory leak on real device only

Open
#938 12 comments 13 reactions 0 assignees View on GitHub
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)
![Capture d’écran 2024-04-10 à 17 01 45](https://github.com/Baseflow/flutter_cached_network_image/assets/37028599/e96613a8-efc3-438c-9712-223a5d070c27)

With Image.network: (stable during scroll)
![Capture d’écran 2024-04-10 à 17 02 46](https://github.com/Baseflow/flutter_cached_network_image/assets/37028599/22e10858-fa39-4952-a4a9-387ce5c84119)

```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.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.