I have written a very simple app in Flutter, for both iOS, Android and the web.
Lately, I realized that if I browse the "storage" settings page on my iPhone, my prod app, as it is distributed on the App Store, weighs about 500 MB. Most of it comes from the "documents and data" part, the app itself isn't huge:
This is unexpected as the only thing I am storing on purpose is a small sqlite database, which in my case is about 30 KB.
Following this guide from the Apple support page, I was able to download my app's container, which is indeed about 500 MB. It turns out most of the weight comes from inside of a tmp
folder, as shown by the output of du -sh AppData/tmp/*
:
0B AppData/tmp/count0fSKCX
0B AppData/tmp/count1N2yFY
32K AppData/tmp/count1tKqnr
0B AppData/tmp/count2BxlSk
24K AppData/tmp/count2VKOVX
0B AppData/tmp/count2tnzwn
[...]
0B AppData/tmp/count8kl1hK
53M AppData/tmp/count8kqOke
0B AppData/tmp/count8ssdC7
[...]
0B AppData/tmp/countZHwkA9
26M AppData/tmp/countZHx1v8
53M AppData/tmp/countZKP9JU
0B AppData/tmp/counta5fYmx
[...]
If I take a look at what's inside of one of those huge 50+ MB folders, here is what takes so much space:
du -sh AppData/tmp/countZKP9JU/count/*
26M AppData/tmp/countZKP9JU/count/main.dart.dill
20K AppData/tmp/countZKP9JU/count/main.dart.incremental.dill
26M AppData/tmp/countZKP9JU/count/main.dart.swap.dill
I failed to find useful documentation about those files, as I am not sure what to look for: is the problem in my Dart config, in my Flutter config, in my App config, ...? Can you guys please enlighten me?
Edit: Here is some version info that might be useful
In case someone runs into the same issue, I ended up writing my own cleanup logic on startup, calling getApplicationDocumentsDirectory().parent.list()
and then deleting the tmp
child folder if it exists.
I don't know if this is necessary at all in the end, as this tmp
folder growing infinitely might only happen because of the development builds I keep installing on my phone. But this cleanup step probably won't hurt anyway.
Edit: Here is the code. Feel free to improve it, I think it probably logs an exception on other platforms (but it doesn't crash):
import 'package:path_provider/path_provider.dart';
[...]
@override
void initState() {
_cleanUpTemporaryDirectory();
super.initState();
}
[...]
_cleanUpTemporaryDirectory() async {
final documentsDirectory = await getApplicationDocumentsDirectory();
documentsDirectory.parent.list().forEach((child) async {
if (child is Directory && child.path.endsWith('/tmp')) {
print('Deleting temp folder at ${child.path}...');
try {
await child.delete(recursive: true);
print('Temp folder was deleted with success');
} catch (error) {
print('Temp folder could not be deleted: $error');
}
}
});
}