Saturday, 6 July 2013

Linux and Java report filesystem space differently

I ran into something interesting this last week. Linux's ext2 filesystem and Java have a different opinion of what free space means, to the tune of 5% difference.

Let's take a look at how each application defines free space. Linux provides a df command that can be used to check free space:

root@system:/$ df Filesystem 1k-blocks Used Available Use% Mounted on /dev/hda2 7834788 2111688 5325092 28% /dev/hda1
 
Filesystem: The name of the partition.
1k-blocks: The total size of the partition, in blocks of 1024 B.
Used : The number of blocks that are used.
Available: The number of blocks that are available for use.
Use %: Equal to [ Used / ( Available + Used ) ].
Mounted on: The logical name of the partition.

Note a couple things here:
First:
Used + Available != Total available.
2,111,688+5,325,092 = 7,436,780

Total - (Used + Available)
7,834,788 - 7,436,780 = 398,008 blocks unaccounted for.

A little bit of research turned up that by default, Linux reserves 5% of the available space (in this case, 400MB) to help ensure the system remains defragmented. The space is hidden under the root user's account, so that even if the filesystem fills up completely, the root user will still be able to login.

Java, on the other hand, can see this 5% hidden space, and it includes it in its free space calculations. Java would calculate the available space as (2111688 + 398008 blocks ) = 2,509,696 blocks free. Java thinks there is 32% free space when there is in reality only 28% usable space.

I used the File.getFreeSpace() and File.getTotalSpace() methods from the Java 6 API to get the disk free values.

If you run a Java application as the root user, the extra hidden space never gets used. df only reports used space of up to 100% used. If the hidden space had been used, the use % column would increase to a maximum of 105% when the entire disk was actually full.

You can use tune2fs to configure the amount of space the ext2 filesystem is allowed to reserve. The -r option will configure the number of reserved blocks.

The lesson learned? Java isn't quite as smart as it pretends to be, and "free" space does not necessarily mean both free and usable.

No comments:

Post a Comment