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

[Web] Release picked file data for Web #1482

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tddang-linagora
Copy link

Issue

There's a memory leak happening with web version

Reproducible code

See here
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: FirstScreen(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const SecondScreen()),
            );
          },
          child: Text('Go to second screen'),
        ),
      ),
    );
  }
}

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

  @override
  State<SecondScreen> createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
  String _name = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("Picked file's name: $_name"),
            TextButton(
              onPressed: () async {
                final picked = await FilePicker.platform.pickFiles(
                  type: FileType.image,
                  withData: true,
                );
                if (mounted && picked != null) {
                  setState(() {
                    _name = picked.files.firstOrNull?.name ?? '';
                  });
                }
              },
              child: Text('Pick image'),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back'),
            ),
          ],
        ),
      ),
    );
  }
}

Steps to reproduce

  1. Run flutter run -d chrome --profile
  2. Open Chrome dev tools, Memory tab, trigger garbage collector then snapshot main heap
  3. Navigate to second screen by clicking on Go to second screen button
  4. Pick a heavy file. In the above code, I chose to pick image because I already have a heavy image (16.3mb)
  5. Go back to the previous screen either by clicking on Go back or any conventional ways for web
  6. Trigger garbage collector then snapshot main heap again. Compare two snapshots.

Expected behavior

The file's data will be deallocated from memory

Actual behavior

The file's data is still on memory

Resolved demo

Before
Screenshot 2024-04-09 at 11 51 30
After
Screenshot 2024-04-09 at 11 53 41

@amrgetment
Copy link
Contributor

as my PR for WASM support got merged you need to update clear children, check the file changes here
https://github.com/miguelpruivo/flutter_file_picker/pull/1481/files
image

@tddang-linagora
Copy link
Author

as my PR for WASM support got merged you need to update clear children, check the file changes here https://github.com/miguelpruivo/flutter_file_picker/pull/1481/files image

Right. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants