Thread: process memory allocation

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    process memory allocation

    Hello all. I have been assigned a school project on detecting memory leaks in linux processes. I am reading.. but have found it hard and inefficient to go through the very vast documentation not knowing what to really look for. Could you please give me some guidelines on this subject?
    Cheers,
    alexcalin

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    can you be more specific about your problem.
    explain the term memory leaks.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    memory leaks can thought of as the failure to properly deallocate memory that was previously allocated. From what I've discovered so far, my main task is to simulate the execution of a program , have a list of all variables that have been given memory space using dynamic allocation and see to it that that space has been properly deallocated when it becomes inaccessible or useless.
    Now, does anyone know a learning resource, a tutorial or something about simulating the execution of a program?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well what you describe in a production scenario is Valgrind Home
    But coming up with that for a school assignment is impractical.

    A very simple memory tracker on the other hand is easy to implement.
    Code:
    void *myMalloc ( size_t size ) {
      void *result = malloc(size);
      // save result in an internal data structure
      return result;
    }
    void myFree( void *ptr ) {
      // locate ptr in the internal structure
      free( ptr );
    }
    At the end of the program, anything remaining in internal store is a memory leak.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    That's ok, but I really have to do it Valgrind style, I know it could be thought of as ambitious or impractical but it is actually a college project (operating systems course ) that I have been assigned and I will receive a mark on it , at the end of the semester. So please help.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    I have to do it by scratch.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well valgrind is open source, so I suggest you get to it and start studying it thoroughly.
    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.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by alexcalin View Post
    That's ok, but I really have to do it Valgrind style
    What Salem posted is how Valgrind does it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    well you can do it in simpler way i think.
    you first take the name of the c programme filename as command line argument and read the whole code into a array using getc().
    u assume at first that there is no loops involved in allocating memory.
    then you search the names of the memory allocating function names like malloc,calloc...
    suppose there are n times occurance of these functions.that means n different memory space is allocated.
    now search for the occurance of the "free(" in that array.
    if threre are less than n occurance you can tell that all alocated memory arent deallocated.

    hope you found my words usefull.
    cheers.
    Last edited by Dibyayan Chakra; 05-02-2011 at 09:27 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Trivial counter example of why that won't work.
    Code:
    int main ( int argc, char *argv[] ) {
      int n;
      int *p;
      if ( argc <= 1 ) {
        n = 10;
        p = malloc( n * sizeof *p );
      } else {
        n = atoi(argv[1]);
        p = malloc( n * sizeof *p );
      }
      // do stuff
      free( p );
      return 0;
    }
    Two malloc, one free, no leak.
    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.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Even more trivial explanation: no guarantee that the names of any function makes it into the binary anywhere other than an address table.

    Soma

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    parse the programme into several section.
    check for patterns like condition statement,loops,functions.
    and then use counter.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dibyayan Chakra View Post
    parse the programme into several section.
    check for patterns like condition statement,loops,functions.
    and then use counter.
    Write a parser for C! That will be taking the LONG, LONG way around. Also it will still not work, because memory allocation is a RUNTIME event:

    Code:
    int bytes;
    unsigned char *buffer;
    printf("Input: ");
    scanf("%d", &bytes);
    buffer = malloc(bytes);
    You simply CANNOT do this by analyzing source code. "Process memory allocation" is about a running process, not source code.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    then how can it be done?
    using the ptrace function might be help full?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dibyayan Chakra View Post
    then how can it be done?
    using the ptrace function might be help full?
    Ptrace is open source and written in C, like valgrind. So you could look at those to find out how they work. I don't know enough about executable formats to say more. I would image the executable needs debugging symbols built into it.

    You could probably also write a kernel module to do this, then nature of the executable would be irrelevant, so you would not need debugging symbols in it. I'm sure this could tell you what blocks were not de-allocated at the end, but I don't think it could provide all the information a user space tool would.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  2. memory allocation
    By pari in forum C Programming
    Replies: 5
    Last Post: 12-10-2002, 10:51 AM
  3. memory allocation
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-09-2002, 01:23 PM
  4. memory allocation
    By Dohojar in forum C Programming
    Replies: 10
    Last Post: 03-15-2002, 11:26 AM
  5. Memory Allocation
    By Yankee in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 03:16 PM

Tags for this Thread