无法使用File delete()方法删除git repo中的.pack文件

sly*_*223 3 java git github jgit

我正在写这个方法,我正在使用jgit库克隆一个git repo,然后用这些文件做一些事情,最后我想删除repo.我遇到的问题是当我在.pack文件(位于.git\objects\pack)上调用delete()方法时,它无法删除.但是可以删除所有其他文件.为什么会这样?

小智 5

刚刚找到了一个干净的方法:用你的repo做完后,用这种方式关闭你的git对象:

Git git;
...
git.getRepository().close();
//then delete files
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 4

您可以看到,基于 JGit 的 Java 应用程序默认会禁用 ddd。例如,
请参阅Gitblit配置文件:

# When true, JGit will use mmap() rather than malloc()+read() to load data from
# pack files. The use of mmap can be problematic on some JVMs as the garbage
# collector must deduce that a memory mapped segment is no longer in use before
# a call to munmap() can be made by the JVM native code.
#
# In server applications (such as Gitblit) that need to access many pack files,
# setting this to true risks artificially running out of virtual address space,
# as the garbage collector cannot reclaim unused mapped spaces fast enough.
#
# Default on JGit is false. Although potentially slower, it yields much more
# predictable behavior.
# Documentation courtesy of the Gerrit project.
#
# SINCE 1.0.0
# RESTART REQUIRED
git.packedGitMmap = false
Run Code Online (Sandbox Code Playgroud)

这与您找到的JGit 线程一致:

我遇到了另一个无法在 Windows 中删除克隆存储库的情况。
这似乎与“ PackedGitMMAP”的使用有关。
这是尝试使用虚拟内存映射时的已知问题吗?

是的。
问题是,在映射被 Java GC 垃圾回收之前,JVM 不会释放内存映射。
这种情况在未来任何时候都可能发生,如果没有足够的内存压力迫使 GC 真正寻找垃圾并回收,则可能永远不会发生。
这就是为什么我们默认禁用该功能,我们无法预测 JVM 何时最终释放该文件。

在此测试用例中,如果我“ setPackedGitMMAP=false”,则存储库已成功删除。

是的。这就是为什么这是默认值。