Provide API to explicitly unmap MappedByteBuffer?

Remi Forax forax at univ-mlv.fr
Tue Mar 20 17:24:22 UTC 2018


I've a class somewhere that guarantee that a clean() will never cause trouble if
- all access are done by the same thread (otherwise you get an exception)
- the wrapper around a MapperByteBuffer has to be stored in a static final to not lost performance.

Technically, making the wrapper constant make the code faster than the usual ByteBuffer code but the thread checking slow the code down,
so in the end, it's more or less as fast as using a ByteBuffer.

Rémi

----- Mail original -----
> De: "Philippe Marschall" <philippe.marschall at gmail.com>
> À: "Langer, Christoph" <christoph.langer at sap.com>
> Cc: "nio-dev" <nio-dev at openjdk.java.net>
> Envoyé: Mardi 20 Mars 2018 17:35:04
> Objet: Re: Provide API to explicitly unmap MappedByteBuffer?

> Hi
> 
> Any serious user I know calls unmap directly. Either through the
> Cleaner in Java 8 or through the new method on Unsafe in Java 9.
> 
> ((sun.nio.ch.DirectBuffer) buffer).cleaner().clean()
> 
> Unsafe.theUnsafe.invokeCleaner(byteBuffer)
> 
> Cheers
> Philippe
> 
> On Tue, Mar 20, 2018 at 11:39 AM, Langer, Christoph
> <christoph.langer at sap.com> wrote:
>> Hi,
>>
>>
>>
>> I was just handling a customer issue where Java code tried to delete a file
>> on Windows which was mapped into memory vial FileChannel.map() before. It
>> took me a little while to understand the issue but obviously the problem was
>> that Windows still kept a delete lock on the file as long as the ByteBuffer
>> was still mapped. This behavior is also documented cleanly:
>> https://download.java.net/java/jdk10/docs/api/java/nio/MappedByteBuffer.html.
>>
>>
>>
>> So far so good, I just started wondering why there isn’t an API to force
>> unmapping (cleanup) on a MappedByteBuffer? The app that opens the mapping
>> simply reads a properties file at this place and I guess they should be able
>> to change this place to use standard io easily and without observable
>> performance problems. However, if they had a way to explicitly unmap the
>> MappedByteBuffer it would also help.
>>
>>
>>
>> Was this topic already discussed? Is there a hard reason why explicit
>> cleanup should not be offered? I guess people using MappedByteBuffers should
>> know anyway what they are doing…
>>
>>
>>
>> Thanks & Best regards
>>
>> Christoph
>>


More information about the nio-dev mailing list