I met a strange case these days.

Scene:

In the process of downloading a file, stop downloading, delete the file, and then download again. (The address of the file is the same, the larger the file, the easier it is to reproduce)

Question:

The delete method returns true if the file was deleted, but the infinite loop shows that the memory space corresponding to the file was not freed.

Solution:

The breakpoint shows that the underlying library that downloaded the file deletes the file before closing the stream when it stops downloading. This order is not correct, so the delete returns true, but the file stream is not closed, so the file space is not freed. An exception will be thrown if the file is created at this time.

Update:

MappedByteBuffer is used to write files. This class is in NIO. It only has map methods, but no unmap methods, so space can only be freed during GC. This class is not suitable for our current business use, so traditional IO operations are used instead.

Please refer to:

Stackoverflow.com/questions/3…