Issue
I wrote an android app that (among other things) writes files to the disk. Now I wonder what's gonna happen when the file system runs out of storage space. I guess that for example java.io.FileOutputStream.write()
will throw an IOException
, which I'll have to catch.
The thing is, I don't want to rely on my super ninja powers - I might have forgotten to put a try-catch somewhere or don't handle it correctly. Thus, I want to test this scenario.
Sadly, I couldn't find any word about a good practice regarding this - not even on SO.
Sure, I cold manually stuff the filesystem with something like this:
size = N
dir = /some/path
data = generateDataForSize(size)
while( write(data, dir) );
while( round(size/=2) > 1 )
write(generateDataForSize(size), dir)
Where write()
will either generate distinct files or append to a single one. There might be some issues with generateDataForSize()
and large sizes but let's put that aside for now.
What's itching me is that I'll have to put this somewhere into my app plus I'll have to clean that $%#! up manually. Okay, if I put it inside its own directory I can just discard the whole thing with a single line on the adb shell.
Anyhow, is there any easier way to go for this that I'm missing? Does android provide any mechanics for this like temporarily limiting the storage space available per app? Any integrated soltions for unit tests or android SDK tools? How do other people (you) do this?
TL;DR: How to efficiently stuff the fs for testing the case of writing to fs when it's full.
edit: I'm testing on a real device that is not rooted. The emulator is deadly slow even with HAXM up and running. Using the emulator is, sadly, not an option.
Tasos Moustakas hint with limiting the space available to AVDs is great, though. I guess if you avoid the issues with android:installLocation="preferExternal"
and move your app to the SD / write files to the limited SD this is a acceptable solution.
The accepted answer is pretty much what I ended up doing. But that still requires some manual work so feel free to post more answers.
Solution
Android runs on linux, so you can go to the shell and use the dd
command to fill up the disk. Connect your device by USB and type adb shell
, then
dd if=/dev/zero of=/sdcard/deleteme bs=1m count=1024
write a 1GB file named deleteme
to the /sdcard
, full of zeroes.
if=
is the input file/dev/zero
just returns endless zeroes.of=
is the output filebs=
is the block size, here one megabyte; some implementations prefer1M
instead.count=
is the number of blocks to write, here 1024, which makes for a 1GB file.
Adjust the sizes to suit your needs, or write multiple files. Prepare for Android to start complaining about low disk space once you hit around 400MB available space. In my experience, Android start complaining about low disk space long before the apps begin to crash.
Answered By - 323go