A little help with understanding what I Have done


I really am just learning after, so please be gentle.

I was trying to save a bit of RAM, and noticed that the Arduino variable Boolean takes up a full byte of memory, as defined in the Arduino specification,

boolean stringComplete = false; // flag to indiacte whether the string is complete boolean tempReadInterval = false; //flag to indicate read interval reached boolean loggingAvailable = false; // flag to indicate if card is available and we can log to card boolean rotateLogFile = false; // flag time to rotate log file name

I therefore decided to try this and save 3 bytes, (yep a bit of a geek and proud :-) with the below code

struct { unsigned int stringComplete : 1; unsigned int tempReadInterval : 1; unsigned int loggingAvailable : 1; unsigned int rotateLogFile : 1; unsigned int leftOver : 4; } flag;

It works no problem.

But, gives me a memory saving of 153 bytes of memory, not just 3 bytes

Further more the original program without the bitfield structure; when started would reduce the memory available from just over 600 bytes to 500 bytes after the first few loops and stay steady at that value. However the new program with the structure starts with 657 Bytes free and stays steady with this amount of free memory, well, at least so far!

As I am learning, I just wonder what has happened to give such a big difference

Just looking to learn


But, gives me a memory saving of 153 bytes of memory, not just 3 bytes

What sort of memory? Is that code memory or SRAM?

Hi Mike,

I thought I mentioned RAM as in the 2048K available on the ATmega 328(also forgot to mention mpu I am using :-( I do get carried away, sorry for the confusion.

All the best


I thought I mentioned RAM as in the 2048K available

Dream on!

I am sorry if I seem to have been insulting, i did feel I had mentioned RAM, which to me is Random Access Memory and not Static Read Only Memory - SRAM, or Electronically Erasable Read Only Memory EEPROM, or Program Memory, which I understand to be flash memory.

I did say I was learning and find it a bit of an insult that a moderator replies with

"dream on"

I have no wish for a fight, my question is genuine, I only want to know an answer, that will help me learn, progress and hopefully promote the Arduino community.

If I have made a mistake please tell, me do not insult me, if you wish to block my comments then please do so, just do not, please, reply with inane comments, at my age I don't need them. I expect to treated with the respect I give to others, in no way did I suggest my post had been wrongly interpreted, I just said I "thought" I had indicted, hence my reply to qualify my question.

I reiterate, please if I make a mistake, just tell me.

All the best


You don't have "2048K" bytes, you have 2048 bytes.

And there I agree with you, it was a typo :-) not wishing to tell you your job :-) but I am going to anyway :-)

"Hey Ian,

we wish we had that much memory on an the Arduino.....

the mod"

I was so annoyed I missed my mistake!

:-) I have seen wars start over such mistakes, hmmm, reminds me of Hitch Hikers Guide to the Galaxy.

All the best


I usually say that you should read everything I write as though it were between tags.
Would it have been better if I’d’ve written "Dream on! :wink: "?

Ian, thanks for pointing out that about the Boolean taking a whole byte each. While I’ve been around micros for years, I’ve only been tinkering with the UNO for about a couple of months. I don’t know why you’d see that much in RAM savings, even the 53 bytes seems unlikely unless you also got rid of some text literals. How are you measuring your free RAM? If you use the String class, I understand you could see some peculiar memory allocation issues.

As for the “insult”, you may wish to try and thicken up your skin and let stuff like that just run off. Believe me, that was nothing compared to what you might experience from some forum “experts”. He’s just nit-picking (and probably joking), and that kinda comes with the territory when dealing with engineering types. As a stereotypical whole, they don’t tend to give off allot of warm fuzzy feelings and often take technical accuracy very seriously; but they don’t usually mean any harm. But, there are some that seem to make a point of being as rudely arrogant as possible, that’s just part of life and you can get that in any public forum. Fortunately, this is a popular tinker-toy and there is a fairly large community present so the signal to noise ratio remains high anyway.

they don't tend to give off allot of warm fuzzy feelings and often take technical accuracy very seriously

Not just technical, I'm a linguist by training - "allot" - verb, to assign as a share or portion. XD

but they don't usually mean any harm


I agree, a lot of what I write is terse, but it is usually to-the-point and indicates fairly accurately where the problem lies, but may require a little effort to interpret. The effort helps to reinforce the learning, I usually find, and what is more fun than learning?

I'm not a linguist, I'm a programmer. I've spent the better part of 35 years intentionally misspelling words to get them to fit, and to coerce the assembler/compiler/interpreter into ignoring them. I do what I can now. ;)

Hi afremon,

I agree it does seem a lot of memory saving for what is a very small change, very strange, I shall continue to research the reasons why.

The code I use for checking memory is below, it is from the book beginning Arduino programming and is credited to Jean-Claude Wippler of JeeLabs

int freeRAM() { extern int heap_start, *brkval; int v; return (int) &v - (brkval == 0 ? (int) &heap_start : (int) __brkval); }

Me being stupid is the quick explanation, old program used IDE 1.03, that will also teach me to use the same board.

Interesting though using the same board to compare I got the following results

with Arduino IDE 1.03 free RAM on first start would reduce from 616 to 579 to 507 bytes, and remain steady at 507bytes, the new version of my code would save exactly 3 bytes as expected compared to the old version, but the memory usage follows the same pattern a loss of 100 bytes of free memory after about 4 seconds of run time.

With Arduino IDE 1.04 free RAM is 654/657 bytes depending on the version of my code I run and this memory usage does not change over time.

Result a saving of 150 bytes of RAM 3 bytes from my code, against 147 bytes RAM obtained by the simple process of an upgrade. If this trend continues I could just not bother to write a program then after not very many IDE upgrades I will find the program I need already in the library and it will use no memory!

All the best


That’s great that the newer release uses less RAM. I think I shall upgrade my stuff now.

I would be interested to know the results with your upgrade, as you know, mileage can vary in this kind of situation, but the knowledge gained, I believe is always worth it.

Is that the "only" thing you changed in your program, to save so much memory ?

michinyon: Is that the "only" thing you changed in your program, to save so much memory ?

He also upgraded his arduino release, that's what really made the difference. Nice to see things taking less space as advancements are made. Maybe they quit trying to stick tone() into everything. ;)