I only know what the JavaDoc says:
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.
and
A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.
You may not be holding onto the buffer, but maybe it's not been GC'd either.
Update: I'll reboot into windows later to try it out, but this wouldn't be a problem on linux.
Update: ...but on windows, yes, that is the problem.
package niolock
import java.nio.channels._
import java.nio.file._
import FileChannel.MapMode.{ READ_ONLY => RO }
import scala.util._
object Test extends App {
val p = FileSystems.getDefault getPath "D:/tmp/mapped"
val c = FileChannel open p
var b = c map (RO, 0L, 100L)
c.close
Console println Try(Files delete p)
b = null
System.gc()
Console println Try(Files delete p)
}
Trying it out:
$ scalac niolock.scala ; scala niolock.Test
Failure(java.nio.file.AccessDeniedException: D:\tmp\mapped)
Success(())
Or:
Release Java file lock in Windows
How to unmap a file from memory mapped using FileChannel in java?