17

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.

1
  • Firstly, where do you test your app? AVD emulator or a physical device? Oct 20, 2015 at 16:23

4 Answers 4

36

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 file
  • bs= is the block size, here one megabyte; some implementations prefer 1M 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.

6
  • 2
    This is pretty much what I ended up with. I connected via adb shell and stuffed the fs with "cat-ting" some trash files around.
    – m02ph3u5
    Oct 22, 2015 at 15:35
  • 2
    I had to use bs=1m instead of bs=1M
    – vman
    Jan 18, 2017 at 2:44
  • 1
    @vman - I also had to use bs=1m instead of bs=1M. I edited the answer to reflect this. Jun 7, 2017 at 18:16
  • 1
    It appears that some implementations expect 1M and others 1m.
    – 323go
    Jun 12, 2017 at 12:37
  • 1
    My samsung device completely ignored the letters and always was in the unit of bytes. My google device used lowercase letters. Definitely completely depends on the device. Nov 7, 2017 at 18:10
2

If you test your app on a AVD emulator, you can simply configure the space available in the emulator. See documentation. Let me know if that solution works for you. Here is your tutorial on how to use AVD for Google Play Services.

5
  • 1
    Thanks for your reply. You should rather put questions as comments that in your answer. The AVD-thing is a great hint, +1 for that. Sadly my app depends on services that are not available inside the emulator.
    – m02ph3u5
    Oct 20, 2015 at 16:20
  • What services are your referring to? Maybe if you share some of your ideas on how you came up to that solution can be very helpful Oct 20, 2015 at 16:29
  • Play services and other gms services.
    – m02ph3u5
    Oct 21, 2015 at 12:02
  • Luckily, you can use AVD for that. See edited answer Oct 21, 2015 at 12:40
  • I updated my answer. The emulator is way too slow to use it for testing on my machine (2007 iMac w/ HAXM & x86 image).
    – m02ph3u5
    Oct 22, 2015 at 14:15
1

Try this: net.namstudio.android.tool.fillmemory.free

-2

If the program being run is only making POSIX system calls you can use libfiu to perform fault injection and things like write calls return ENOSPC to simulate disk full.

Another trick is to use a FUSE filesystem such as charybdefs that allows you to inject faults at a filesystem level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.