Will shared memory allocated in Unix get destroyed automatically if you don't do that ?
e.g one process created that segment, another process take use of it. now they're both crashed for some reason, was that segment causing a memory leak ?
Will shared memory allocated in Unix get destroyed automatically if you don't do that ?
e.g one process created that segment, another process take use of it. now they're both crashed for some reason, was that segment causing a memory leak ?
Generally not.
If you're using SysV shared memory (e.g, shm_open
), then no. None of the SysV IPC primitives will clean themselves up; you always have to explicitly clean them up, or do so manually with ipcrm
. This is one of several reasons that I recommend strongly against using them.
If you're mmap
ping a file to get shared memory, that file obviously won't go away on its own. Like any other file, you'd have to explicitly unlink
it to make it go away. If you've already done that, then you're all set.
It depends on the persistence you give the segment. As a rule of thumb, if you give it a name through shm_open
(or a key) it will be persistent. As that indicates it will persist after you close the process that created it, unless you somehow "delete" it, that is do an shm_unlink
. This is the only way you can share segments between completely different processes.
So if you do that you really have to take care to clean things up. The best strategy for such persistent segments is to have all processes open an file descriptor initially, and then to unlink as soon as everybody is connected. The segment will exist until the last file descriptor to it is closed.
Non-persistent segments can be mapped anonymously directly with mmap
. They will go away as your process ceases to exist.
No. Allocated shared memory segments should be discarded explicitly.
Useful command line tools for listing of shred memory segments and removing:
ipcs
and ipcrm
.
The last one can be used for ex. for removing a segment when all programs,using it, have crashed.