Thread: Checking for memory leaks

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Checking for memory leaks

    Is there a way to track a programs memory usage so I can make sure I don't have a memory leak issue?

    What I'm hoping for is some sort of function like mem_usage() or something that will return the amount of memory my program is using.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are tools like Valgrind and Purify that can perform such memory leak detection.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you're using Microsoft visual something-or-other, then read this
    http://msdn.microsoft.com/en-us/library/bebs9zyz.aspx
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Quote Originally Posted by laserlight View Post
    There are tools like Valgrind and Purify that can perform such memory leak detection.
    Thanks, quiet helpful, how about C-Lint?
    Just GET it OFF out my mind!!

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    lint only does static code analysis. if you use a library function where the source isn't available, lint can't tell you if the function allocated memory and you forgot free it. if it were that simple you could just write a macro wrapper around malloc and free to keep track of heap memory.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Meldreth View Post
    lint only does static code analysis. if you use a library function where the source isn't available, lint can't tell you if the function allocated memory and you forgot free it. if it were that simple you could just write a macro wrapper around malloc and free to keep track of heap memory.
    And I doubt that lint can follow code that allocates and frees memory in different functions - at least not well enough to know if it's ALWAYS freed or not.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. checking for memory leaks
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2003, 06:42 PM
  5. command in unix for checking memory leaks
    By sanju in forum Linux Programming
    Replies: 1
    Last Post: 01-22-2003, 06:51 AM