Using Probe 1 and 2 (for advanced users)

Modified on Tue, 27 Dec, 2022 at 3:50 PM

For those of you needing to dig deeper in their code to find the minimum Free RAM value reached, you are free to place Probe 1 and 2 anywhere in your program (inside functions, inside your libraries…). 


To put a probe, include library HC_Sram.h and use the supplied variable called “HC_sram”:

  • HC_sram.setProbe(probe)    =>   performs SRAM measurement
#include <HITIComm.h>
#include <HC_Sram.h>

...

void function1()
{
    // measure SRAM on Probe 1
    HC_sram.setProbe(1);

    // code which consumes RAM
}

void function2()
{
    // measure SRAM on Probe 2
    HC_sram.setProbe(2);

    // code which consumes RAM
}


Where to place my probes?

You might be surprised that sometimes the probes give you the same values while they are not at all at the same place. So how is that possible? 


First, this is because of the way the memory is loaded and managed in your Arduino. Secondly, your compiled code might not exactly reflect your source code because of code optimization made by your compiler. 


Let’s see this through 2 typical cases:


1) If you put your probes inside a same function, even at different places inside this function, they will give you the exact same values. Explanation: memory required by a function is loaded when entering this function and is kept all along the life of this function, so your probes will see the same amount whatever their position is inside the function. As a result, only one probe is required per function, just put it at the beginning.

void loop()
{
    // Probe 0
    HC_communicate();

    // ...
    // your code
    // ...

    // Probe 1 will measure the same values as Probe 0
    HC_sram.setProbe(1);
}

 

2) if you put a probe inside a function f1 and another one inside a function f2, with function f2 being directly called by function f1, measured values by these 2 probes may be the same. This may happen if function f2 is not used much in your program, that is if it is called once or twice only. In this case, the compiler optimizes the code by deleting function f2 and directly copying its content inside the calling functions, like f1. The resulting compiled code have the 2 probes inside the same function f1. And we then fall on case 1).


Before optimization:

void f1()
{
   // Probe 1
    HC_sram.setProbe(1);

    f2(); // f2 is only used here
}

void f2()
{
    // Probe 2
    HC_sram.setProbe(2);

    // f2 code
}


After optimization (compiled code):

void f1()
{
    // Probe 1
    HC_sram.setProbe(1);

    // Probe 2
    HC_sram.setProbe(2);

    // f2 code
}






Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article