Results 1 to 7 of 7

Thread: [RESOLVED] Retrieve memory size of a quick app

  1. #1
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 06
    Location
    *Stack Trace*
    Posts
    1,512

    Resolved [RESOLVED] Retrieve memory size of a quick app

    Hi,

    I have this little app that executes under 0,2 seconds and I'd like to know what the memory usage is while running.
    For example, when timing it, I use: time ./main
    Is there something similar for reading memory usage?

    Thanks
    Delete it. They just clutter threads anyway.

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 06
    Location
    *Stack Trace*
    Posts
    1,512

    Re: Retrieve memory size of a quick app

    So I've been trying a couple of things and this seems to work.
    Code:
    ./main | ps v -C main
    This does what I want it to do, but is there a better way?
    Delete it. They just clutter threads anyway.

  3. #3
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 02
    Location
    #!/bin/bash
    Posts
    5,645

    Re: Retrieve memory size of a quick app

    I'm not at my linux box right now but I'd have a look in the /proc directory, under the directory that is the same as the process id of your app.

    E.g.: /proc/1234

    That will contain a lot of real-time data about your app.
    I don't live here any more.

  4. #4
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 06
    Location
    *Stack Trace*
    Posts
    1,512

    Re: Retrieve memory size of a quick app

    Yeah figured that, but the problem is that the app exits in 0,2 seconds.
    Now I've added a sleep(180) and that gave me some time to explore the proc data.
    'status' contains VmPeak and VmSize what I think is what I'm looking for.

    Also a bit off-topic, I was wondering whether C has a limit in array size?
    It seems that it fails when I try to make an array of four million items, although the memory usage is 20MB tops.
    Now mind you, I just started C; I have a lot to learn
    Delete it. They just clutter threads anyway.

  5. #5
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 02
    Location
    #!/bin/bash
    Posts
    5,645

    Re: Retrieve memory size of a quick app

    The language doesn't have any such limit (although common sense does, but that will only be obvious to those with a great deal of C experience ).

    Consider the following statement, it is syntactically legal but in practice, utterly insane...

    Code:
    char buffer[4 * 1024 * 1024]; //create a 4MB array on the stack
    ...This will attempt to define a block of memory on the stack (which has a finite size - you've heard of stack overflows right?). The stack isn't very big, this is true on all platforms, Linux, Windows, Mac etc.

    As a guideline I never allocate more than about 2 kilobytes on the stack (that is, at the top of a function definition). If you define two variables then the amount of stack space you've used is a sum of the two sizes. In other words:

    Code:
    int a;
    int b;
    ...on a 32bit machine would take 8 bytes of stack space.

    So any time C coders need a lot of storage space they use the malloc() function instead. I won't bother to explain that here (this isn't the C forum after all), but malloc() allocates a block of memory on the "heap" (aka the "free store").

    When you've finished with that block of memory you MUST use the free() function to tell the OS that you've finished with it, otherwise that is a "memory leak" (that memory will be inaccessible to any other app until your own app has terminated).

    Malloc, calloc, realloc and free are all very simple to use but you must use them with caution.

    For all intents and purposes there isn't any upper limit to the amount of ram you can malloc() apart from the total memory in your system.

    It's worth noting that malloc (and co) won't permanently gobble up ram. When your app terminates, the OS will recover all memory blocks that were mistakenly left malloc()ed at the end.

    Hope that's helpful

    It's probably worth asking about this subject in the C forum as well, other people will have views about this too.

    Almost all my C experience is Linux based, so I'm slightly biased. But I'd be happy to carry on talking about it here if you like.
    Last edited by wossname; Apr 14th, 2010 at 01:22 PM.
    I don't live here any more.

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 06
    Location
    *Stack Trace*
    Posts
    1,512

    Re: Retrieve memory size of a quick app

    That's certainly helpful
    Stack is like memory in standby for processing right?

    Anyway I'm taking my time for C as I'm probably going to get Java the next couple of years...

    The app I mentioned earlier is a problem from Project Euler (it's a project for bored geeks).
    The particular problem had a great performance boost with memorizing processed values, hence my curiosity.
    It worked fine with an array of a million longs (according to the proc info 4MB), so I was happy
    (yes, I know, not great practice )

    I guess the first step to memory allocation and stuff is to learn about pointers, right?
    I'll first dig in to that; will let you know if I have big difficulties with that.
    Speaking of which; do you know any good tutorials for that?

    Thanks
    Last edited by TheBigB; Apr 14th, 2010 at 05:53 PM.
    Delete it. They just clutter threads anyway.

  7. #7
    Frenzied Member tr333's Avatar
    Join Date
    Nov 04
    Location
    /dev/st0
    Posts
    1,426

    Re: Retrieve memory size of a quick app

    If you are looking for a C reference, I would highly recommend getting the K&R book written by the language designers. I've yet to find anything (books/internet/whatever) that explains stuff as well as this does.
    Don't forget about rep points if you think a post has benefited you in any way.
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •