[solved] freeRam() does not provide reliable results

From http://playground.arduino.cc/Code/AvailableMemory I learned how to use freeRam().

In the following example freeRam() returns wrong values:

in my sketch:

      Serial.print(F("freeRam vor dispWD:"));Serial.println(gsm.freeRam());

in a separate file EPD_grapp.cpp the function ist implemented:

void dispWindData(boolean printout) {
  // global: winddir[], windspeed[], rbuf_idx,rbuf0, wmin_val,wmax_val,
  //          wmin_disp,wmax_disp, xpos
  int ws; 
  word wmin,wmax;

Serial.print(F("freeRam in dispWD:"));Serial.println(gsm.freeRam());

  ws = windspeed[rbuf_idx];

I wanted to measure the amount of RAM used for the function call.
The result is:

freeRam vor dispWD:3106
freeRam in dispWD:3106

The second value should be much less than 3106 because with the function call some basic registers are saved on the stack and the local variables also take some bytes of RAM.

The only idea I have about the source of error is that in my project freeRam() is declared and implemented as a method of a class, GSM_GP. The instantiation of the class is GSM_GP gsm.
The reason for that is that I want to use freeRam() at several locations within my project. And the module containing the class GSM_GP is included everywhere.

Can this be the problem?
Has somebody experience with using freeRam() in a way described above?

It would be more helpful if you would provide simplified, complete code demonstrating what you're talking about, rather than snippets and vague descriptions.

You should also mention which board you're running the code on and which version of the hardware package for that board you're using.

Have you tried using the MemoryFree library? If so, did you encounter different results?

I can't tell without seeing your code but it may be that the compiler is optimizing away the things that you expect to use memory. Arduino AVR Boards 1.6.12 and newer are especially good about doing this due to LTO(link time optimization). For example, you expect a function call to use memory but the function may be automatically inlined.

Please use the chain link icon on the toolbar to make links clickable.

The board is a Mega2560. There is no version number visible on the back side. But a part of the back side is masked by some mechanics. I bought the board 1 year ago.
The IDE is 1.6.12.

I did'nt try MemoryFree library because some postings on this forum told me that this library needs additional RAM, and I had a lot of work to reduce RAM usage to the momentary level.

Perhaps compiler optimization is the reason that within the first function call there is the same amount of RAM available as before the function call.
The first function is calling a second one, and within the second function the available RAM is less, as expected. This again argues for compiler optimization.
On the other hand I would be astonished if optimization makes a function with about 40 lines of code inline. But nevertheless it could be because this function is called only once in my sketch.

On the other hand I would be astonished if optimization makes a function with about 40 lines of code inline.

Is the function used more than once? The latest compiler supports "link time optimization", which is pretty aggressive. It seems to regularly inline setup() and loop() into main(), for example... (making disassembly a PITA to look at.)

ok, thank you for your tips.

Now I am quite sure that it is compiler optimization, which now means that freeRam() is returning correct values.