Thread: Telling how much free memory there is

  1. #1
    Just
    Guest

    Telling how much free memory there is

    Hi people,

    I seem to have an impossible-to-fix memory leak in my program. To try to fix it, I'd like to be able to tell how much memory is currently available, so I can check if I'm freeing stuff properly at different points in the program. Anyone know how to do this?

    I've tried using system("free") commands (I'm using Linux), but that doesn't seem to work - the amount of free memory listed doesn't change directly after a malloc.

    I'd be grateful for any suggestions...

    Thanks,

    Justin

  2. #2
    Just
    Guest
    Thanks a lot for your reply Salem. From that page you gave is a link to a program called ccmalloc, which I tried out. And it doesn't seem to work very well when it comes to detecting out of bounds errors (guess electric fence is the way to go for that), but it's great for memory leaks. Thanks again!

    Just

  3. #3
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Make sure you initialize all pointers to zero before using them. After you free a pointer, initialize it to zero again.

    That way, if you try to use a pointer that's been free- you should crash.

    Secondly, at the end of the program, look at all your pointers to see if they are zeroed. if not, you know you missed a free() call somewhere, and you know which pointer(s) you missed.

    You need a debugger to this easily.

    Every malloc() must have a complimentary free().

    This is a trivial issue.
    It is not the spoon that bends, it is you who bends around the spoon.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One way to know how much memory you have free, is to keep calling malloc until malloc fails. When malloc fails, you know you don't have any memory free.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  3. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  4. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 05:16 PM
  5. free allocated memory on interrupts
    By scrappy in forum C Programming
    Replies: 4
    Last Post: 02-20-2004, 11:13 AM