how to measure free RAM
| Sat, 2008-04-26 19:09 | |
|
Hi all I want to get the free memory available when running my program (to write to log file), so I can track it better. The reason I have to do this is because afaik Carbide does not have a nice memory debugging tool. And my program always gets kern-exec 3 error code Is there some library call where i can get this figure? Or better, still , is there any nice tool to do memory debug on Symbian? Any replies will be greatly appreciated. Thanks for reading. Cheers! Codito Ergo sum |
|







Forum posts: 1058
What makes you think that filling up all available memory is the reason for your problem? A "kern-exec 3" can have many different reasons.
Did you already debug and check where the error occurs? (Not with some kind of "memory debugger", as you asked, but with the "normal" runtime debugger.)
René Brunner
Forum posts: 6
Hi rbrunner,
First of all, I know that my app would takes lots of memory, coz I'm porting one PC application, which takes something like 20M on PC.
Anyway, it dies at a memcpy function, which is within my library. I can't find any other clue than guessing that it's full memory.
Cheers
Codito Ergo sum
Forum posts: 1058
Well, correct me if I am wrong - but memcpy does not allocate any memory, it just copies it, right? I don't see how memory could get full at the execution of a memcpy, then. Are you sure your pointers are ok, and enough destination memory allocated?
Anyway, a quick use of forum search (box top right) for "free memory amount" gave me the following:
http://www.newlc.com/topic-3151
René Brunner
Forum posts: 1134
kern-exec 3 is quite common when porting stuff that takes a lot of memory.
reason is quite simple, a memory allocation fails, and the orginal app doesn't handle all memory allocation problems, so a pointer ends up being null.
then someone (for example a memcopy) tries to copy stuff into it, and bam, you panic with kern-exec 3.
If your app needs 20MB of memory, you will have to increase the default heap size in your application.
You are supposed to be able to do this by specifying the EPOCHEAPSIZE keyword in your mmp-file, but if that doesn't work for you, then you can try add this in your E32Main:
TInt E32Main()
{
RHeap* newheap = User::ChunkHeap(NULL,300000,25000000); //smallest size 300Kb, grow to 25 MB
RHeap* oldheap = NULL;
if(newheap)
{
oldheap = User::SwitchHeap(newheap);
}
return EikStart::RunApplication(NewApplication);
}
Be adviced though that 20MB is _very_much_ for a phone application, and you might have trouble finding a phone it runs on at all.
Forum posts: 6
Thank you all,
Sorry to bring up this topic again. Been away for a short while.
I have followed the thread that rbrunner suggested and found User::Available(TInt &a).
In the document it says: Returns "The total free space currently available on the current thread's heap."
I have User:Available() to be 316964 at some point. Does thi mean that I have 316964 BYTES(or what) available free RAM at that time?
thanks again
Codito Ergo sum
Forum posts: 1134
No.
It just means you have that number of bytes currently free on your current threads heap.
It does NOT even mean that an allocation for a bigger block then "avaialable" will fail, because your heap migh expand, if you have specified that it is allowed to.
As I wrote in my previous post, you can change heapsize either in the mmp file with EPOCHEAPSIZE, or by useing the code I posted...
you WILL have to change heap size if you are to allocate things as big as 20MB.
easiest way to know if there is room enough in RAM for a heap with 20MB of space is probably to try create one as described in my previous post.
put minsize to 20MB and if there is space enough for it, it will be created, if not, it will fail, and you will have to try find some other phone to run on, or optimize ram usage.
Forum posts: 6
Duplicate post
Codito Ergo sum
Forum posts: 6
Thanks alh for your advice, but it's not what i want yet.
I simply want to moniter how much memory my program has used (including mainly heap + stack size and all that) at a few points during execution. I used to think this shouldn't be so complicated for such an advanced OS...........Why why why
Codito Ergo sum
Forum posts: 1134
Oh, why didn't you say so from the start?
Tint User::AllocSize(TInt &aTotalAllocSize) will return in the argument, the total number of bytes in your heap at this point.
add it to User::Available() and you will see how big your heap chunk is currently.
To measure stack usage, you can save a pointer to a stack variable in the very begininig of your program, and then compare this to a pointer of any stack variable later.
along these lines:
TInt gStackAddress; TInt E32Main() { TInt firststackpos; gStackAddress = (TInt)&firststackpos; // [...] Rest of entrypoint function } TInt GetBytesOnStack() { TInt dummy; return gStackAddress - (TInt)&dummy; }Forum posts: 6
Thanks alot alh. I got the stacksize calculation but not sure about heap. Does this mean that I have to use User:Alloc in stead of all the malloc() i'm currently using in order to be able to measure current heap size?
I searched the docs on User::AllocSize and User::Available but not much info. Can you, if possible give me a sample code? My dll has lots & lots of malloc and memcpy and all that...
Thanks a gain
Codito Ergo sum
Forum posts: 1134
malloc will "under the hood" result in a call to User::Alloc, since that is the native symbian API for memory allocation, so this will work for that too.
"numbytesonheap" would be the total sum of bytes allocated in all calls to malloc.
"numbytesonheap + available" would be an approximate size of the heap chunk, but a more exact number can be got through:
This is the number of bytes actually allocated to your program at this point, "numbytesonheap" is just how many bytes currently in use.
"biggestblock" is the biggest continuous block in the heap. If you try to allocate a bigger block then this in one call, then the heap will have to try to grow, even if there is more bytes "available".
If "biggestblock" is a lot smaller then "available" that could mean heap fragmentation caused by bad ordering of your malloc and free-calls, creating lots of small "holes" in the heap, and therefore make your program use more memory then it really needs.
Its important to remember that "available" is just the number of bytes currently available _without_heap_resizing_. The heap will automatically grow when you try to allocate more then "available", until it reaches its max size.