shared memory not getting freed

This is a discussion on shared memory not getting freed within the Linux Programming forums, part of the Platform Specific Boards category; I have a program that allocates shared memory for itself and then forks and attaches the same shared memory to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487

    shared memory not getting freed

    I have a program that allocates shared memory for itself and then forks and attaches the same shared memory to the child process. it looks like the shared memory is not getting freed after both the parent and child detach and call shmctl(shm_id, IPC_RMID, NULL), but when I use the command ipcs -m in a shell, all of the IDs I allocated still show up, but show that they are unattached. I'm not sure what more I need to do to free these shared memory segments. any help would be greatly appreciated.

    thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems like this is the intention:
    from http://fscked.org/writings/SHM/shm-5.html#ss5.3

    Shared segments in System V are a bit different than those obtained with BSD's mmap. First of all, while you do need a key for a segment which is usually obtained by running ftok(2) on some file in the filesyetem, System V shared segments exist only in memory. Because of this, System V shared segments must persist after the termination of a process. It is the responsibility of the programmer to ensure that all of his segments are removed from the system, otherwise his memory leaks become system wide.
    My emphasis.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    as I'm sure you read in my original post, I am detaching and calling shmctl() to remove the shared memory. it does not seem to be producing the desired effect. is there anything else that I need to do, in addition to these two operations, to free the shared memory?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you are doing that in BOTH of the forks?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    Quote Originally Posted by matsp View Post
    And you are doing that in BOTH of the forks?

    --
    Mats
    yes, I am. according to the docs, this is all that is required, but I still see shared memory not getting freed.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Are you checking return codes for failure?

    gg

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    Quote Originally Posted by Codeplug View Post
    Are you checking return codes for failure?

    gg
    I wasn't but now I am, and now it appears the memory is being freed.... weird... I'll monitor it for a few days and see what happens, but it appears to be working properly now.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    one other thing.... is there a way to free the memory without a reboot? or do I just have to accept the lost memory until the next reboot?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    See if "ipcrm" helps any.

    gg

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    Quote Originally Posted by Codeplug View Post
    See if "ipcrm" helps any.

    gg
    I tried it, but for every ID I try to remove, it says ipcrm: already removed id (...)

    any other ideas?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    I'm having a completely new problem with this now...

    for some reason, over the last 24 hours or so, it has failed to delete about 60 shared memory segments, all of which say they are still attached, although the program did not report a failure to detach, but rather a failure to free the shared memory after a detach (this behavior is not completely unexpected, as I'd expect it to throw an error when trying to delete a segment that has already been deleted).

    I am calling shmdt() and shmctl(shmid, IPC_RMID, NULL) in both processes using the shared memory, so I am really confused about what is going on here. I know there have been many more than 60 connections to the server since then, so it's clearly not doing it for every shared memory segment, but I don't really know what's going on for the ones it's failing to delete.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    It's called SHARED MEMORY. If your program deleted the segment when it exited, it would hardly be shared, would it?

    This is one of the few resources that does NOT get reclaimed when you exit. Personally, I stay the hell away from shared memory and just use shared memory maps.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,487
    Quote Originally Posted by brewbuck View Post
    It's called SHARED MEMORY. If your program deleted the segment when it exited, it would hardly be shared, would it?
    This is one of the few resources that does NOT get reclaimed when you exit.
    did you read any of the previous posts in this thread?

    at what point did I say I was allocating memory and then exiting? and even if that was the case, if I detach the memory from all processes that have it attached, and then delete it, it should be reclaimed by the system, not left hanging.

    I'm detaching and deleting the segments from all processes that attach them at any point, and some of them are not getting freed after I tell the system to free them. in fact all of the ones that failed to delete are showing as being still attached after I tell the system to detach them.

    this is certainly not an case where I am just allocating shared memory arbitrarily and then exiting and expecting the operating system to clean up after me. I am following the procedures recommended in the documentation, so I don't understand why some of the shared memory is remaining after I attempt to delete it.

    if you really were trying to be helpful, it certainly didn't come across that way. I see that you have over 2900 posts, and I'd imagine that you're pretty well respected on this forum, but in my opinion, you really didn't contribute anything useful to the thread. in fact, your post seemed more condescending than anything. I'm not some n00b wanna-be hacker playing with my parents' computer. I have quite a few years of experience writing code for many platforms, including windows, linux/unix, and mac, so I have some idea what I'm doing.

    if you have any helpful advice, I'd be glad to receive it, but your post really seemed like you were just trying to tell me that I don't understand the concepts of what shared memory is, or how the system handles it.

    if I've misunderstood or mischaracterized your post, I apologize, but please offer constructive advice for how to solve my problem, rather than just telling me what you don't do, and reprimanding me for something that isn't even related to this thread.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Elkvis View Post
    did you read any of the previous posts in this thread?
    at what point did I say I was allocating memory and then exiting?
    That's pretty much verbatim what you said.

    if I've misunderstood or mischaracterized your post, I apologize, but please offer constructive advice for how to solve my problem, rather than just telling me what you don't do, and reprimanding me for something that isn't even related to this thread.
    I did offer constructive criticism, which you were too busy being indignant to notice. My suggestion is to not use SHM and use shared memory maps instead.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Are you calling shmget()/shmat() in the parent and child? Before or after fork()?

    gg

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 01:29 AM
  3. shared memory can not read value over 255
    By jbsloan in forum C Programming
    Replies: 4
    Last Post: 04-03-2005, 11:56 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21