Search code examples
javamemory-managementjvmdiskspace

deleted files remains open consuming huge disk space


I am running java processes.But that consumes huge amount of disk space keeping some deleted files opened. The location of the files is /tmp (i.e. while i am not exclusively creating those files). What can be the reason for such occurrences and how to overcome it?


Solution

  • This can happen for instance on Linux when a process is still using a file while another process deletes that file. The file will appear as deleted, but under the hood, the kernel will not free up the space as long as any process is still using the file descriptor.

    To release the space, you have to tell the program to close the file descriptor. This depends entirely on the program design and might not even be possible. For example, if this was a word processor, you would choose File->Close in main menu.

    If you are unable to "tell" the program to close the file, your final option is to terminate the process.

    If you like playing around, you can also attach to process with a debugger such as gdb, find the descriptor and close it manually. Note that this might cause the program to crash or behave unexpectedly.

    In all cases where the file is abruptly closed, its contents will likely be corrupted.

    I don't think this can happen on Windows because Windows uses different locking semantics and will not allow a file to be deleted if it's open.