Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between getUsableSpace and getUnallocatedSpace of FileStore class

Tags:

java

nio

I've read the definition in the documentation and performed some searches on the Internet, but it is still not clear to me. What's the difference between getUsableSpace() and getUnallocatedSpace() in the FileStore class?

like image 246
Rafael Avatar asked Apr 30 '13 14:04

Rafael


2 Answers

From the FileStore class documentation

getUnallocatedSpace() Returns the number of unallocated bytes in the file store.

getUsableSpace() Returns the number of bytes available to this Java virtual machine on the file store.

So there is possibly more unallocated space than usable space.

You can test it with the following code snippet

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;

public class TestFileStore {
    public static void main(String[] args) throws IOException {
        for (FileStore fileStore : FileSystems.getDefault().getFileStores()) {
            System.out.println(fileStore.name());
            System.out.println("Unallocated space: " + fileStore.getUnallocatedSpace());
            System.out.println("Unused space: " + fileStore.getUsableSpace());
            System.out.println("************************************");
        }
    }
}

This is an excerpt of my output

************************************
tmpfs
Unallocated space: 206356480
Unused space: 206356480
************************************
/dev/sda6
Unallocated space: 1089933312
Unused space: 790126592
************************************
like image 62
Ortomala Lokni Avatar answered Oct 20 '22 01:10

Ortomala Lokni


From peeking at the documentation, I would assume that getUsableSpace is oriented toward the current java vm, while getUnallocatedSpace refers to all unallocated space on the file store.

like image 24
Dolphiniac Avatar answered Oct 20 '22 00:10

Dolphiniac