【问题标题】:Where is the JVM holding my file lock?JVM 在哪里持有我的文件锁?
【发布时间】:2023-03-30 20:07:01
【问题描述】:

我正在处理 java.nio.file.AccessDeniedException 问题。

我有一个 Scala 程序,如果我这样做:

java.nio.file.Files.delete(FileSystems.getDefault().getPath("""D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]\1977 - Foreigner\03 - Starrider.mp3""")) 

一切正常。我有一些我做的代码

def delete(path : Path) {
  try {
    println("deleting " + path)
    java.nio.file.Files.delete(path)
  } catch {
    case exception: Exception => System.err.println(exception)
  }
}

val google1 = FileSystems.getDefault().getPath("""D:\Users\Eric\Google Drive\Music\Downloaded\Foreigner [Discography HQ]""")
val google2 = FileSystems.getDefault().getPath("""D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]""")

val duplicates = TraversablePaths(List(google1, google2)).duplicateFilesList

println("deleting duplicate files")
duplicates.foreach(_.filter(!_.startsWith(google1)).foreach(delete))

但是当我尝试删除同一个文件时,我得到了

java.nio.file.AccessDeniedException: D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]\1977 - Foreigner\03 - Starrider.mp3

我能说的最好的情况是 JVM 要么锁定文件,要么锁定文件所在的目录,但我不知道在哪里。检查文件是否相同的代码如下所示

def identical(file1 : Path, file2 : Path) : Boolean = {

  require(isRegularFile(file1), file1 + " is not a file")
  require(isRegularFile(file2), file2 + " is not a file")

  val size1 = size(file1)
  val size2 = size(file2)

  if (size1 != size2) return false

  var position : Long = 0
  var length = min(Integer.MAX_VALUE, size1 - position)

  val channel1 = FileChannel.open(file1)
  val channel2 = FileChannel.open(file2)

  try {
    while (length > 0) {
      val buffer1 = channel1.map(MapMode.READ_ONLY, position, length)
      val buffer2 = channel2.map(MapMode.READ_ONLY, position, length)
      if (!buffer1.equals(buffer2)) return false
        position += length
    length = min(Integer.MAX_VALUE, size1 - position)
    }
    true
    } finally {
    channel1.close()
    channel2.close()
  }
}

我原以为关闭通道会释放 JVM 需要的任何文件锁。这是我实际打开文件进行读取的代码的唯一部分,尽管代码的其他部分确实检查文件长度,但我不希望 JVM 需要文件锁。

JVM 持有文件锁的其他原因是什么?我怎样才能知道,我怎样才能释放它们?

干杯,埃里克

【问题讨论】:

  • 这并没有真正回答您的问题,而是搜索 Process Explorer。您可以搜索文件名的句柄,它通常会为您提供持有锁的进程。在这种情况下,它会回答 Java 是否有锁或其他进程是否有。
  • 只有一个进程在运行:JVM。可以重复的是,我可以从 Scala 中单独删除文件,除非文件是从搜索重复文件的代码中返回给我的,所以我无法想象会有任何其他进程持有锁。好主意,不过,谢谢你的提示。

标签: java scala jvm filelock


【解决方案1】:

我只知道 JavaDoc 是怎么说的:

映射一旦建立,就不再依赖于文件通道 用于创建它。尤其是关闭通道并没有 影响映射的有效性。

一个映射的字节缓冲区和它所代表的文件映射仍然存在 在缓冲区本身被垃圾回收之前有效。

您可能没有保留缓冲区,但也可能没有被 GC。

更新:我稍后会重新启动到 windows 来尝试一下,但这在 linux 上不会有问题。

更新:...但是在 Windows 上,是的,这就是问题所在。

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)
}

试一试:

$ scalac niolock.scala ; scala niolock.Test
Failure(java.nio.file.AccessDeniedException: D:\tmp\mapped)
Success(())

或者:

Release Java file lock in Windows

How to unmap a file from memory mapped using FileChannel in java?

【讨论】:

  • OK,通过取消映射 MappedByBuffer 解决了问题。既然我知道内存映射文件的问题,想到我编写的所有代码都可能有缺陷,因为我没有显式地取消映射缓冲区,我感到畏缩。在我看来,MappedByteBuffer API 存在严重缺陷,因为它不支持取消映射缓冲区的 close() 方法。顺便说一句,非常感谢您的回答。这是一次真正的学习经历。
猜你喜欢
  • 1970-01-01
  • 2015-12-20
  • 2012-06-10
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 2016-06-01
相关资源
最近更新 更多