Hasan Al-Baghdadi
Hasan Al-Baghdadi

Reputation: 442

In Julia, How do you delete a file after creating it for memory-mapping?

I've trying to work with large datastructures that store more than what ram could handle and I've had to use mmap is a result.

This all works as expected, however at the end of the code I would want to delete this temporary file as it is no longer used.

I've tried using rm to delete the file or manually deleting it while the file is running, but I don't have the permission to do so.

This is what my code looks like.


using Mmap

io = open("example.tmp", "w+")
v = Mmap.mmap(io,Vector{Int64},Int64(1e9))
close(io)

#Perform some actions on vector v

#attempt to delete here, rm("example.tmp") returns an EACCES error though

I want to be able to have this .tmp file be deleted automatically when no longer in use, how would I go about doing this?

Upvotes: 1

Views: 638

Answers (1)

carstenbauer
carstenbauer

Reputation: 10127

using Mmap

io = open("example.tmp", "w+")
v = Mmap.mmap(io,Vector{Int64},Int64(1e9))
close(io)

#Perform some actions on vector v

v = nothing # delete reference to memory mapped data
GC.gc() # call garbage collector to be safe

rm("example.tmp") # should work now

As long as there is a reference to the memory mapped data you might run into permission issues. After all you could still be using v. Try to delete/overwrite any reference to the data by setting v = nothing and calling GC afterwards.

Upvotes: 1

Related Questions