Pages: [1] 2   Go Down
Author Topic: A little help with understanding what I Have done  (Read 915 times)
0 Members and 1 Guest are viewing this topic.
Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Ian
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ian

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought I mentioned RAM as in the 2048K available
Dream on!
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ian
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ian
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I usually say that you should read everything I write as though it were between <tongue-in-cheek>  </tongue-in-cheek> tags.
Would it have been better if I'd've written "Dream on!  smiley-wink "?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.





Logged

Experience, it's what you get when you were expecting something else.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.   smiley-lol

Quote
but they don't usually mean any harm
True.

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?

Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-wink

 
Logged

Experience, it's what you get when you were expecting something else.

Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
    }
Logged

Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ian
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Experience, it's what you get when you were expecting something else.

Haiti
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: