Thread: memory occupied by a program

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    memory occupied by a program

    hi all,
    i wanted to know(just for curiosity) can we write any code which gives us the memory ocupied by any other c-program.so that if we have two different codes written for the same algo ,we can compare which one is better memory wise.
    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well that all depends on your OS and compiler.
    Generally, "yes" is the answer, but there's no set method.
    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.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    You should just run the algorithm in a program that keeps track of how many bytes of memory it allocates. Much easier doing it from the inside out than the outside in, especially considering your programs can't access the memory of another program directly.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A great deal here would depend on what granularity of precision you need. Windows Task manager and Linux/Unix ps or top will show you memory usage, but it's not showing a great deal of precision, so if you need to know EXACTLY (or within a few dozen bytes or so), then you would, as IceDane suggests, have a "hacked" version of malloc/free and/or new/delete (and their collegues) to track memory usage.

    --
    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
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    matsp,can u plz tell me how can i see memory usage using task manager.i'm using windows xp and turbo c++.also what do u mean by "hacked" version of malloc/free.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    matsp,can u plz tell me how can i see memory usage using task manager.i'm using windows xp and turbo c++.also what do u mean by "hacked" version of malloc/free.
    In task manager, in the tab called "Processes", you'd see a column saying "Mem Usage". If you don't, make the window wider. If that's not showing it, try View->Select Columns and select "Memory usage". You may also want to use "Peak memory usage".

    As to hacked malloc/free, I mean that you use methods that replace the malloc and free with your own versions, that track the amount of memory used (a macro replacement that translates "malloc" to "mymalloc", etc. is the simplest form).

    Also, if you use Turbo C as a DOS executable, then you probably won't see which uses the most memory, because of the way that DOS deals with free memory. There may be a function in Turbo C libraries to "get amount of free memory" - I remember Turbo Pascal had such a feature.

    --
    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. Memory allocation overflow in c program
    By bassam_fci in forum C Programming
    Replies: 6
    Last Post: 05-19-2009, 03:35 PM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM