Windows: moving a file that was previously mapped in memory fails

前端 未结 2 1128
长情又很酷
长情又很酷 2020-12-22 02:45

-:= EDITED TO SIMPLIFY =:-


I am facing an issue in the process of porting code from a Linux (Ubuntu LTS 12.4) environment to Windows Server 2008.

I n

2条回答
  •  情话喂你
    2020-12-22 03:12

    In Java, file mappings are garbage collected, and there is no supported way to forcibly destroy a mapping.

    From the FileChannel.map() documentation:

    The buffer and the mapping that it represents will remain valid until the buffer itself is garbage-collected.

    A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping.

    In Sun's JDK, you can test that this is indeed the culprit by forcibly destroying the mapping just before doing the file move:

    import sun.nio.ch.DirectBuffer;
    import sun.misc.Cleaner;
    [...]
    if (byteBuffer.isDirect()) {
        Cleaner cleaner = ((DirectBuffer) byteBuffer).cleaner();
        cleaner.clean();
    }
    // move file
    

提交回复
热议问题