I am debugging this problem with Ian so I am going to jump in here. I ran the program under strace
to see what is happening (kernel 5.8.15-201.fc32.x86_64
). Here are my findings.
When the SharedArray is created, it gets a shared memory object, mmaps it, and unlinks it.
openat(AT_FDCWD, "/dev/shm/jl326782ohMyKWxhO9Rm4KiyE8Qr", O_RDWR|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600) = 29
mmap(NULL, 1365245952, PROT_READ|PROT_WRITE, MAP_SHARED, 29, 0) = 0x7f0c6ac6f000
munmap(0x7f0c6ac6f000, 1365245952) = 0
close(29) = 0
openat(AT_FDCWD, "/dev/shm/jl326782ohMyKWxhO9Rm4KiyE8Qr", O_RDWR|O_NOFOLLOW|O_CLOEXEC) = 32
fcntl(32, F_GETFL) = 0x28002 (flags O_RDWR|O_LARGEFILE|O_NOFOLLOW)
mmap(NULL, 1365245952, PROT_READ|PROT_WRITE, MAP_SHARED, 32, 0) = 0x7f0c1926f000
unlink("/dev/shm/jl326782ohMyKWxhO9Rm4KiyE8Qr") = 0
The region starting at 0x7f0c1926f000
is not munmaped until the program exits. It is not munmaped when the GC runs. The last line in the program (write(22, "DONEDONEDONEDONEDONEDONEDONEDONE", 32DONEDONEDONEDONEDONEDONEDONEDONE) = 32
) runs before this munmap call
munmap(0x7f0c1926f000, 1365245952) = 0
I am confident that when the GC runs the SharedArray is finalized because the ref put in SharedArrays.sa_refs
is deleted, which only happens in the finalizer finalize_refs
. Finalizing the SharedArray should also finalize its member s
, though. Mmap.mmap
registers a finalizer for s
that calls munmap
, so I don’t know why this is not called.