There is a difference between free space and usable space!
You are working on Java code that involves checking if there is enough disk space available to your Java app to complete some task.
You look at the File class for ways to do this. You notice the getFreeSpace() method. And you use it to do your checks for the disk space.
Voops! That may work. But it may not. Depends on your operating system and environment configuration.
You should actually be using the getUsableSpace() method. This method does a few additional checks, where possible, about write permissions and quota available to the user. Also, on some systems a certain percentage of total disk space is reserved for admin/root users. This can make your checks for space pass incorrectly if you happen to use the getFreeSpace() method. And you won’t even find out until it starts affecting your app in a different environment on your prod boxes.
Now that you’ve read this, don’t make that mistake. Use the getUsableSpace() method. And send your colleagues a link to this article to make sure they know to do so too.
Related posts:
- Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError
- Java NullPointerException Ninja – 10 facts you need to know to avoid problems with null
Tags: Java
