Skip to main content
pkumar1883
Associate III
August 16, 2016
Solved

Calculating size of used RAM at run time

  • August 16, 2016
  • 2 replies
  • 1063 views
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.
    This topic has been closed for replies.
    Best answer by troy1818
    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). 

    2 replies

    troy1818
    troy1818Best answer
    Associate III
    August 16, 2016
    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). 

    Tesla DeLorean
    Guru
    August 16, 2016
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..