MUnmap Mmapped file with Finalizer

98 views
Skip to first unread message

Tamás Gulácsi

unread,
Mar 26, 2021, 7:11:58 AM3/26/21
to golang-nuts
Hi,

I'm trying to create a nice api for slurping an io.Reader into memory (if size is under threshold), or into a temporary file that's Mmaped.

Everything works, if I give back an explicit io.Closer beside the []byte of the data, that Unmaps the []btye region when the caller has finished.

I wanted to get rid of that io.Closer, and use runtime.SetFinalizer(&p,...)

But that p is collected when not in use - even when a subslice is in use!

I've tried to runtime.SetFinalizer(&p[0]),
but that complains that that address is not an allocated - which is understandable, as that's allocated by the OS, not by the Go GC.

So, how to Unmap only when all the references of the backing array gets garbage collected?

Or any other idea?

Thanks in advance,
Tamás Gulácsi

Brian Candler

unread,
Mar 26, 2021, 8:01:59 AM3/26/21
to golang-nuts
Is it even necessary?  Doesn't Go take care of unmapping the mmap'd region when the object is garbage collected?
If that's not true, can you show some more of your code to explain the problem?

Tamás Gulácsi

unread,
Mar 26, 2021, 9:23:57 AM3/26/21
to golang-nuts

But mainly I use it at https://github.com/tgulacsi/go/blob/master/i18nmail/walk.go#L72 so all I want is an io.Reader,
thus your example of golang.org/x/exp/mmap returning an io.ReaderAt may be enough, thanks!
Reply all
Reply to author
Forward
0 new messages