Tuesday, June 17, 2008

Deleting shared memory segments

Recently, I found myself using IPC in a multi-threaded perl script that I wrote.

After running the script a few times I eventually ran out of memory segments and so the script would just die (turns out that the script wasn't robust enough ;-)

So after searching around, I came up with 2 commands that go through and clean, (read delete) all segments (and semaphores) on the computer. The commands need to be run from the command line.

On Solaris:

ipcs -s | awk ' $5 == "ekawas" {print $2, $5}' | awk '{ print $1}' | while read i; do ipcrm -s $i; done

ipcs -m | awk ' $5 == "ekawas" {print $2, $5}' | awk '{ print $1}' | while read i; do ipcrm -m $i; done


On Linux/Unix:

ipcs -s | awk ' $3 == "ekawas" {print $2, $3}' | awk '{ print $1}' | while read i; do ipcrm sem $i; done

ipcs -m | awk ' $3 == "ekawas" {print $2, $3}' | awk '{ print $1}' | while read i; do ipcrm -m $i; done


Of course, you will need to change "ekawas" to a more appropriate username. To find out the username that you should be using, run 'ipcs -s' or 'ipcs -m' and see who created the segments.

Please be careful, because all segments created by all users will be listed; so don't delete ones that you didn't create!

No comments: