Thursday, September 15, 2016

Easiest way to simulate disk full error and out of memory error in a program in Linux

What is /dev/full?

 /dev/full (aka Always Full device) is a special file in Linux, which always returns "No space left on device"  error when a program or a person tries to write to this file. It also provides infinite number of NULL characters when a program tries to read from this file.

The main purpose of this file is for testing programs against "Disk Full" error.

Disk Full Error

The easiest way to demonstrate the "Disk Full" error is by using "echo" command to write something to "/dev/full" file. For testing the "Disk Full" condition in your program, you can try to write the data to "/dev/full" file like you do for writing to a normal file.



Out of Memory Error:

 When you read the /dev/full file, it provides infinite number of NULL characters. Therefore if you use a function which read until the new line (\n) character, it will read the characters to memory until it gets an Out of Memory error. For example, I have created following Java program which uses java.io.BufferedReader.readLine() method. Because this readLine() method read the null characters from the /dev/full without finding any new line characters, the heap space becomes full and it throws java.lang.OutOfMemoryError.


Output: