cancel
Showing results for 
Search instead for 
Did you mean: 

Calculating size of used RAM at run time

pkumar1883
Associate II
Posted on August 16, 2016 at 09:56

Hi all,

Is there a way to calculate the size of used RAM at running time. So that I can calculate how much RAM is still available for use. Actually I have to show used RAM in percentage to my product customer.
1 ACCEPTED SOLUTION

Accepted Solutions
troy1818
Senior
Posted on August 16, 2016 at 11:51

To answer this questions it is necessary to give a clear definition about the words ''ram'' and ''used''.

Ram usually consist of heap and stack memory. If you are using some operating system it should be possible to calculate how much stack that was allocated for each process, however that will lead us to the question above of what ''used'' really means. When it comes to heap it will get a little more difficult. If you have control over the malloc and free functions is could be possible to keep some counter here tracking the allocated memory (given that malloc is always used to allocate memory). To count the stack usage you could also just subtract the stack pointer from the start of the stack (probably found in link script). 

View solution in original post

2 REPLIES 2
troy1818
Senior
Posted on August 16, 2016 at 11:51

To answer this questions it is necessary to give a clear definition about the words ''ram'' and ''used''.

Ram usually consist of heap and stack memory. If you are using some operating system it should be possible to calculate how much stack that was allocated for each process, however that will lead us to the question above of what ''used'' really means. When it comes to heap it will get a little more difficult. If you have control over the malloc and free functions is could be possible to keep some counter here tracking the allocated memory (given that malloc is always used to allocate memory). To count the stack usage you could also just subtract the stack pointer from the start of the stack (probably found in link script). 

Posted on August 16, 2016 at 14:57

There are usually linker symbols you can apply or use to determine the initial RAM (statics) usage. You can walk the heap via the linked list describing the arena there (dynamic), and you can measure the maximal stack depth (local/auto).

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..