I'm using a factory method to instantiate a class derived from a base class based on a parameter. The paramater is used i n switch() statement to determine which of several derivatives are instantiated and returned.
What i see is that the free memory decreases when I add more possible derived class to the switch statements!
If I compile the script beneath with FULL_SWITCH defined, the outcome si: Minimum Memory Usage: 492 bytes (6% of a 8192 byte maximum)
If I compile the same script wit FULL_SWITCH undefined, the outcome is: Minimum Memory Usage: 286 bytes (3% of a 8192 byte maximum)
Can anybody explain this? The whole purpose of the factory method is to avoid having all objects in memory at the same time!
#include "MemoryFree.h"
//#define FULL_SWITCH
class Base2
{
public:
const char c[200] = "verylongstring verylongstring verylongstring verylongstring lkjdsl;aksdj a;sja;sdf a;sd fadsj fasdh adadssss";
virtual int getInt() { return 0; }
};
class Der1 : public Base2
{
public:
const char b[200] = "verylongstring verylongstring verylongstring verylongstring ";
int i = 100;
virtual int getInt() { return i; }
};
class Der2 : public Base2
{
public:
int i = 200;
};
class Der3 : public Base2
{
public:
int i = 300;
};
class Der4 : public Base2
{
public:
int i = 400;
};
class Factory
{
public:
static Base2* makeBase(int i) {
switch (i) {
case 0: return new Der1(); break;
#ifdef FULL_SWITCH
case 1: return new Der2(); break;
case 2: return new Der3(); break;
case 3: return new Der4(); break;
#endif // FULL_SWITCH
default:
return NULL;
break;
}
}
};
Base2* b = nullptr;
Base2* b2 = nullptr;
void setup()
{
Serial.begin(115200);
Serial.println(String(freeMemory()));
b = Factory::makeBase(1);
Serial.println(b->c[1]);
Serial.println(String(b->getInt()));
Serial.println(String(freeMemory()));
b2 = Factory::makeBase(4);
Serial.println(b2->c[2]);
Serial.println(String(b2->getInt()));
Serial.println(String(freeMemory()));
}
void loop()
{
/* add main program code here */
}
I suspect you're looking at RAM usage, which INCREASES when you compile in those other classes, and DECREASES when you don't. Certainly the CODE for that program is much more than 496 bytes...
But, since you didn't provide a complete, compileable program....
RayLivingston:
I suspect you're looking at RAM usage, which INCREASES when you compile in those other classes, and DECREASES when you don't. Certainly the CODE for that program is much more than 496 bytes...
But, since you didn't provide a complete, compileable program....
Regards,
Ray L.
I dont quite understand why you say its not a complete compilable program ... Its the exact sketch I've compiled and run .. and then again .. when adding code the amount of SRAm shouldnt decrease just because of that
bascy:
I dont quite understand why you say its not a complete compilable program ... Its the exact sketch I've compiled and run
Except you have the "MemoryFree" include file, and we don't....
bascy:
and then again .. when adding code the amount of SRAm shouldnt decrease just because of that
With all due respect, the compiler does not agree with you, and it's usually right. If you look at the link map, you'll probably be able to figure out why...
Ok... memoryFree wasnt partv of it but it wasn't part of the problwm either as others already noticed
Moving on...
Please explain to me why adding code could decrease SRam .. as i understand it code is flashed into "Progmem" ... no need to take up very scarse and expensive SRAM.
Maybe adding code could make the optimizer to include our exclude objwct instances but I still don't see how this code could do this
I don't know the answer to this problem but, taking account of the amount of time spent on it already I can't help wondering why you don't just create a single global char array and populate it from PROGMEM as needed.
Robin2:
I don't know the answer to this problem but, taking account of the amount of time spent on it already I can't help wondering why you don't just create a single global char array and populate it from PROGMEM as needed.
...R
This is just an example of what i encountered in a very large project where based on an id objects are created. The object are large (>250 bytes) and there are 12 to 15 different classes to create them from but only one of them is needed in memory an any given time. When i created then all static my SRAM is full
So, besides that i want to know why this is costing SRAM... im looking for a solution....
holmes4:
"a very very very long string is also stored in RAM not in prom unless you use the F() macro"
YOUR ASSUMPTIONS ABOUT WHAT THE COMPILER DOES ARE WRONG. CHANGE THEM LOOK IN THE DOCUMENTATION FOR DETAILS OF WHAT IT DOES.
Mark
No need to shout... but you are right about the strings... should have F ()-ed them.
So the definition of the derived classes are removed because they are not used... and so is the string which is in SRam.
Will try to produce an example without the string (and the switch) just to verify
bascy:
The object are large (>250 bytes) and there are 12 to 15 different classes to create them from but only one of them is needed in memory an any given time.
I think you missed the point I was trying to make.
Why not create a single global char array and arrange that whatever class is needed puts its data into it rather than trying to create its own data space.
The strings in the example are just there to give the objects created from the classes some volume. The real situation doesn't contain those. There the classes contain numerous other objects.
But they did break my example because i forgot to add the F ()