Pages: [1]   Go Down
Author Topic: Block comments don't give me back my memory?  (Read 526 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I started running out of memory and as I uncommented some code it seemed as if I wasn't getting my memory back and I noticed something.  If I use block comments the code size doesn't decrease completely and the memory used doesn't completely decrease.  If I use line comments I get my memory returned correctly


Arduino IDE 1.0.2

Free memory code
Code:
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

extern unsigned int __heap_start;
extern void *__brkval;

/*
 * The free list structure as maintained by the
 * avr-libc memory allocation routines.
 */
struct __freelist {
  size_t sz;
  struct __freelist *nx;
};

/* The head of the free list structure */
extern struct __freelist *__flp;

#include "MemoryFree.h";

/* Calculates the size of the free list */
int freeListSize() {
  struct __freelist* current;
  int total = 0;

  for (current = __flp; current; current = current->nx) {
    total += 2; /* Add two bytes for the memory block's header  */
    total += (int) current->sz;
  }

  return total;
}

int freeMemory() {
  int free_memory;

  if ((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__heap_start);
  } else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
    free_memory += freeListSize();
  }
  return free_memory;
}




Binary size 2,434 bytes
Memory left 1837
Code:
//#include <IRremote.h>
#include <MemoryFree.h>
//#include <IrCommons.h>
//#include <Enums.h>
//#include <Commons.h>
//#include <SoftwareSerial.h>
//#include <RadioBlock.h>
//#include <TrueRandom.h>
//#include <EEPROM.h>
//#include <IrCommands.h>
//#include <RfCommons.h>

void setup(void) {
  Serial.begin(9600);
}

void loop() {
  Serial.println(freeMemory());
}




Binary size 2434
Memory left 1837
Code:
#include <MemoryFree.h>

void setup(void) {
  Serial.begin(9600);
}

void loop() {
  Serial.println(freeMemory());
}




Binary size 2898
Memory left 1769
Code:
/*#include <IRremote.h>*/
#include <MemoryFree.h>
/*#include <IrCommons.h>
#include <Enums.h>
#include <Commons.h>
#include <SoftwareSerial.h>
#include <RadioBlock.h>
#include <TrueRandom.h>
#include <EEPROM.h>
#include <IrCommands.h>
#include <RfCommons.h>*/

void setup(void) {
  Serial.begin(9600);
}

void loop() {
  Serial.println(freeMemory());
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to get some more info

Seems using /*bla*/ does comment out the line correctly also, just like //
Seems #include <SoftwareSerial.h> has something in it that block comment might not comment out.  Not sure what else block comment doesn't remove correctly

This gives memory back
Code:
/*#include <IRremote.h>*/
#include <MemoryFree.h>
/*#include <IrCommons.h>
#include <Enums.h>
#include <Commons.h>
/*#include <SoftwareSerial.h>
#include <RadioBlock.h>
#include <TrueRandom.h>
#include <EEPROM.h>
#include <IrCommands.h>
#include <RfCommons.h>*/

This doesn't
Code:
/*#include <IRremote.h>*/
#include <MemoryFree.h>
/*#include <IrCommons.h>
#include <Enums.h>
/*#include <Commons.h>
#include <SoftwareSerial.h>
#include <RadioBlock.h>
#include <TrueRandom.h>
#include <EEPROM.h>
#include <IrCommands.h>
#include <RfCommons.h>*/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So seems since the files where compiled before they are still being picked up even when commented out using block comments

C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=102 -IC:\Users\user\Dropbox\arduino-1.0.2\hardware\arduino\cores\arduino -IC:\Users\user\Dropbox\arduino-1.0.2\hardware\arduino\variants\standard -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\MemoryFree -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\Enums -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\Commons -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\RadioBlock -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\TrueRandom -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\EEPROM -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\IrCommands -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\RfCommons -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\WeaponHelper -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\LiquidCrystal -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\WaveHC -IC:\Users\user\Dropbox\arduino-1.0.2\libraries\Bounce C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WeaponFirmware.cpp -o C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WeaponFirmware.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\MemoryFree\MemoryFree.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Commons\Commons.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\RadioBlock\RadioBlock.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\TrueRandom\TrueRandom.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\EEPROM\EEPROM.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\RfCommons\RfCommons.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WeaponHelper\WeaponHelper.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\LiquidCrystal\LiquidCrystal.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WaveHC\FatReader.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WaveHC\SdReader.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WaveHC\WaveHC.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WaveHC\WaveUtil.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Bounce\Bounce.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WInterrupts.c.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring.c.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_analog.c.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_digital.c.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_pulse.c.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_shift.c.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\CDC.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\HardwareSerial.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\HID.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\IPAddress.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\main.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\new.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Print.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Stream.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Tone.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\USBCore.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WMath.cpp.o
  Using previously compiled: C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WString.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\WInterrupts.c.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring.c.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_analog.c.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_digital.c.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_pulse.c.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\wiring_shift.c.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\CDC.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\HardwareSerial.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\HID.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\IPAddress.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\main.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\new.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Print.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\core.a C:\Users\user\AppData\Local\Temp\build8362834629383853962.tmp\Stream.cpp.o
C:\Users\user\Dropbox\arduino-1.0.2\hardware\tools\avr\bin\avr-ar rcs
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see there is a 1.0.3 IDE.  Downloading and will try that shortly
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think 1.0.3 has an even bigger problem

With the includes with line comments the sketch size is 2512
Code:
//#include <IRremote.h>
#include <MemoryFree.h>
//#include <IrCommons.h>
//#include <Enums.h>
//#include <Commons.h>
//#include <SoftwareSerial.h>
//#include <RadioBlock.h>
//#include <TrueRandom.h>
//#include <EEPROM.h>
//#include <IrCommands.h>
//#include <RfCommons.h>


Block comments make the sketch 2976
Code:
//#include <IRremote.h>
#include <MemoryFree.h>
/*#include <IrCommons.h>
#include <Enums.h>
#include <Commons.h>
#include <SoftwareSerial.h>
#include <RadioBlock.h>
#include <TrueRandom.h>
#include <EEPROM.h>
#include <IrCommands.h>
#include <RfCommons.h>*/


No comments the sketch is 3556
Code:
#include <IRremote.h>
#include <MemoryFree.h>
#include <IrCommons.h>
#include <Enums.h>
#include <Commons.h>
#include <SoftwareSerial.h>
#include <RadioBlock.h>
#include <TrueRandom.h>
#include <EEPROM.h>
#include <IrCommands.h>
#include <RfCommons.h>
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With block comments:
Code:
/*
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
*/

void setup() {
}


void loop() {
}
ls sketch_jan18a.cpp.hex -l
2640  sketch_jan18a.cpp.hex

ls sketch_jan18a.cpp.elf -l
93226  sketch_jan18a.cpp.elf


With line comments:
Code:
//#include <SoftwareSerial.h>
//#include <LiquidCrystal.h>


void setup() {
}


void loop() {
}

ls sketch_jan18a.cpp.hex -l
1335  sketch_jan18a.cpp.hex

ls sketch_jan18a.cpp.elf -l
25235  08:34 sketch_jan18a.cpp.elf
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the pre-compile stage scans the #include lines of the sketch to detect which libraries are used thus what object files to compile and include in the final executable:

This is the compile line of the "block comment" version:
Code:
hardware/tools/avr/bin/avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p -o /tmp/build1844011101910210169.tmp/sketch_jan18a.cpp.elf /tmp/build1844011101910210169.tmp/sketch_jan18a.cpp.o /tmp/build1844011101910210169.tmp/SoftwareSerial/SoftwareSerial.cpp.o /tmp/build1844011101910210169.tmp/LiquidCrystal/LiquidCrystal.cpp.o /tmp/build1844011101910210169.tmp/core.a -L/tmp/build1844011101910210169.tmp -lm
notice SoftwareSerial.cpp.o and LiquidCrystal.cpp.o

This is instead the compile line for the "lien comment" version:
Code:
hardware/tools/avr/bin/avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p -o /tmp/build1844011101910210169.tmp/sketch_jan18a.cpp.elf /tmp/build1844011101910210169.tmp/sketch_jan18a.cpp.o /tmp/build1844011101910210169.tmp/core.a -L/tmp/build1844011101910210169.tmp -lm
Logged

Pages: [1]   Go Up
Jump to: