heap memory release policy in Arduino

#include <Arduino.h>
#include "include/MainComponent.h"

/*
  Turns on an LED on for one second, then off for one second, repeatedly.
*/


MainComponent* mainComponent;

void setup()
{

    mainComponent = new MainComponent();
    mainComponent->beginComponent();

}

void loop()
{

    mainComponent->runComponent();
}

is there any callback to release memory in Arduino ?(e.g to call delete mainComponent)

or this will happen automatically as the loop ends?

is there any callback to release memory in Arduino ?

There are no callbacks. There are mechanisms like free(), delete, and letting the object go out of scope (which will call the destructor).

or this will happen automatically as the loop ends?

Nothing in that snippet goes out of scope when loop() ends.

Ok , then what is the strategy to ensure freeing the memory allocated in that code snippet?

SCENARIO :"I wanted to access the object in both methods , so the object is declared in the global scope then instantiated at setup."

What happen when loop() terminated ? will mainComponent still remain in the memory?

If it was in OS NO , process will terminated then resources will be deallocated.

So in Arduino how can I achieve above SCENARIO , by ensuring memory will be deallocated when the controller is switched off ?

Ok , then what is the strategy to ensure freeing the memory allocated in that code snippet?

Turn off the power.

SCENARIO :"I wanted to access the object in both methods , so the object is declared in the global scope then instantiated at setup."

There is no particular advantage, in that code, to calling the constructor in setup(). Nor is there any good reason in that code for using new and a pointer (over just creating an object).

What happen when loop() terminated ?

It gets called again.

will mainComponent still remain in the memory?

Of course.

So in Arduino how can I achieve above SCENARIO , by ensuring memory will be allocated when the controller is switched off ?

What do you care what was in volatile memory when that memory looses power? There is nothing that you need to do to ensure that volatile memory is wiped/deallocated/freed when power is lost.

nish1013:
What happen when loop() terminated ? will mainComponent still remain in the memory?

Of course it will. It's a global pointer and you have not deleted the object it points to.

Quote
SCENARIO :"I wanted to access the object in both methods , so the object is declared in the global scope then instantiated at setup."
There is no particular advantage, in that code, to calling the constructor in setup(). Nor is there any good reason in that code for using new and a pointer (over just creating an object).

This is an e.g to get clarify the scenario. It is obvious for this e.g that is not necessary. So please consider this as a prototype of an advanced scenario.

Quote
So in Arduino how can I achieve above SCENARIO , by ensuring memory will be allocated when the controller is switched off ?
What do you care what was in volatile memory when that memory looses power? There is nothing that you need to do to ensure that volatile memory is wiped/deallocated/freed when power is lost.

OK , that means it doesn't matter how many heap memory objects I have created on global scope when Power-loss all gone.

It is obvious for this e.g that is not necessary.

Well, it was obvious to me. I didn't know whether it was obvious to you.

OK , that means it doesn't matter how many heap memory objects I have created on global scope when Power-loss all gone.

On power loss, all volatile memory is cleared. Technically, that happens on reset, but the point is that you don't have to worry about it.

The "it doesn't matter how many heap memory objects I have created" part of that statement bothers me, though. The Arduino has limited amounts of memory, used for global variables, literals, the heap, and the stack. You can't create as many heap objects as you want, unless the number you want to create is reasonable.

Insert Quote

Quote
It is obvious for this e.g that is not necessary.
Well, it was obvious to me. I didn’t know whether it was obvious to you.

Sorry , it was my fault , I would have been more clear on the question.

Quote
OK , that means it doesn’t matter how many heap memory objects I have created on global scope when Power-loss all gone.
On power loss, all volatile memory is cleared. Technically, that happens on reset, but the point is that you don’t have to worry about it.

The “it doesn’t matter how many heap memory objects I have created” part of that statement bothers me, though. The Arduino has limited amounts of memory, used for global variables, literals, the heap, and the stack. You can’t create as many heap objects as you want, unless the number you want to create is reasonable.

Agree with you regarding limited memory. I think I need to understand different between OS based and Micro-controller memory mgt.
Is there any recommend resource?

In programming terms they are the same (you are basically dealing with C++ memory management, nothing more) but you just need to deal with the fact that in an Arduino the available memory is very limited.

In programming terms they are the same (you are basically dealing with C++ memory management, nothing more) but you just need to deal with the fact that in an Arduino the available memory is very limited.

Perfect , so to summarize,

We will be thinking still in terms of Stack/Heap and general concepts will be the same at Arduino.

If any objects are created in the global scope will be deleted when power-loss .

Please see my comment in the below code

#include <Arduino.h>
#include "include/MainComponent.h"

/*
  Turns on an LED on for one second, then off for one second, repeatedly.
*/

MainComponent mainComponentNotInHeap; // in global scope ever living until power loss??


MainComponent* mainComponent;

void setup()
{

    mainComponent = new MainComponent();
    mainComponent->beginComponent();

}

void loop()
{

    mainComponent->runComponent();
}
MainComponent mainComponentNotInHeap; // in global scope ever living until power loss??

Actually, no. The compiler will see that you never use mainComponentNotInHeap, so it will not actually allocate space for it.

If you did use mainComponentNotInHeap, then, yes, the comment is correct.

nish1013:
We will be thinking still in terms of Stack/Heap and general concepts will be the same at Arduino.

If any objects are created in the global scope will be deleted when power-loss .

If you're used to dealing with processes running within an operating system then you can think of a running Arduino as being analogous to a process. When you power up or reset an Arduino, the sketch starts from a clean state very similar to the way a process starts up from a clean state: your global and static variables will be initialised by the C++ runtime framework and then the main() function will be called; you don't usually get to see the Arduino main() function but basically it initialises the hardware and associated parts of the Arduino runtime framework, calls setup() once and then calls loop() repeatedly. Every time you restart the Arduino, it's like killing a process running in an OS and then creating a new process.