Click to See Complete Forum and Search --> : [RESOLVED] Retrieve memory size of a quick app
TheBigB
Apr 11th, 2010, 08:51 AM
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 :wave:
TheBigB
Apr 11th, 2010, 10:23 AM
So I've been trying a couple of things and this seems to work.
./main | ps v -C main
This does what I want it to do, but is there a better way?
wossname
Apr 14th, 2010, 11:36 AM
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.
TheBigB
Apr 14th, 2010, 12:11 PM
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 ;)
wossname
Apr 14th, 2010, 01:18 PM
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...
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:
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 :D
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.
TheBigB
Apr 14th, 2010, 05:50 PM
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 (http://projecteuler.net/) (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 :D
(yes, I know, not great practice :afrog:)
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
tr333
Apr 17th, 2010, 06:56 AM
If you are looking for a C reference, I would highly recommend getting the K&R (http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)) book written by the language designers. I've yet to find anything (books/internet/whatever) that explains stuff as well as this does.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.