Truncate memory mapped file

后端 未结 2 1802
眼角桃花
眼角桃花 2021-02-19 21:39

I am using memory mapped IO for an index file, but the problem is that I\'m not able to resize the file if it is mostly empty.

Somewhere before:

MappedB         


        
相关标签:
2条回答
  • 2021-02-19 22:13

    Your issue is that you are using unreliable method to close mapped byte buffer (one hundred calls to System.gc() and System.runFinalization() don't guarantee you anything). Unfortunately there is no reliable method in Java API to do that, but on Sun JVM (and perhaps on some others too) you can use the following code:

    public void unmapMmaped(ByteBuffer buffer) {
      if (buffer instanceof sun.nio.ch.DirectBuffer) {
        sun.misc.Cleaner cleaner = ((sun.nio.ch.DirectBuffer) buffer).cleaner();
        cleaner.clean();
      }
    }
    

    Of course it is JVM-dependent and you should be ready to fix your code if Sun ever decides to change sun.nio.ch.DirectBuffer or sun.misc.Cleaner in an incompatible manner (but actually I don't believe this will ever happen).

    0 讨论(0)
  • 2021-02-19 22:16

    This is just a supplement to the previous answer, which is completely correct.

    JDK 1.7 complains about the use of sun.misc.Cleaner, saying that classes in this namespace are not a formal part of the JDK, and may disappear in the future. However, as of 1.7 they are still present.

    If the .clean() method is unavailable, then using System.gc() can be used as a fallback method, however this must be acknowledged to be a "hack" and care must therefore be used.

    While System.gc() cannot force an unreferenced mapping to be closed, in practice it will often cause cleanup to happen. Experience on 32-bit Linux (and Solaris) shows buffers being released during every test during either the first or second call to System.gc(). However, the behavior on Windows is different. In most cases, all mappings are released by the end of the second call to System.gc(), but sometimes it requires 3 calls. There are still occasions where more calls are required, with a requirement for a higher number of calls diminishing in frequency. This can be deceptive, in that tests may indicate that 4 calls are all that is required, only to have it fail on you a month later. 5 calls may then seem adequate, only to lead to failure in 6 months.

    Testing to see if a map has been released can be done by using a try/catch block around FileChannel.truncate(), with a loop to re-attempt the operation on failure. The loop cannot be infinite, as there are pathological cases where a particular heap configuration will lead the garbage collector to never clean up a mapping. However, a loop of about 10 will cover almost all cases. If the object isn't gone by that point, then it's not going anywhere and the application will have to give up. That may seem inadequate, but in practice, it is extremely unlikely, and will only be an issue on a JVM that doesn't support cleaners.

    0 讨论(0)
提交回复
热议问题