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![]()
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.
So I've been trying a couple of things and this seems to work.
This does what I want it to do, but is there a better way?Code:./main | ps v -C main
Delete it. They just clutter threads anyway.
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.
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.
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...
...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.Code:char buffer[4 * 1024 * 1024]; //create a 4MB array on the stack
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:
...on a 32bit machine would take 8 bytes of stack space.Code:int a; int b;
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.
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.
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,