我正在编写的方法是使用jgit库克隆git仓库,然后对这些文件进行一些操作,最后想要删除该仓库。 我遇到的问题是,当我调用.delete()方法来删除一个.pack文件(位于.git\objects\pack目录下)时,它无法被删除。虽然其他文件都可以被删除。为什么会出现这种情况呢?
我正在编写的方法是使用jgit库克隆git仓库,然后对这些文件进行一些操作,最后想要删除该仓库。 我遇到的问题是,当我调用.delete()方法来删除一个.pack文件(位于.git\objects\pack目录下)时,它无法被删除。虽然其他文件都可以被删除。为什么会出现这种情况呢?
我刚找到了一个干净的方法来做到这一点: 在对您的仓库进行操作后,以以下方式关闭您的git对象:
Git git;
...
git.getRepository().close();
//then delete files
git.close()
和git.getRepository().close()
不是相同的!Javadoc似乎暗示git.close()
也会关闭底层存储库,但这并非总是如此,因此最好明确关闭它。 - hopper# 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