Skip to content

Commit 7c87db3

Browse files
committed
Detect RAM usage and stop if full
This resolves issue arduino#1356 and add the ability for the Arduino IDE to detect the amount of RAM allocated to a sketch and compare that to the available RAM on each board. If RAM is more than 90% full, it will fail on building since there is not enough free RAM for the heap and stack to use.
1 parent 443d0e1 commit 7c87db3

File tree

4 files changed

+95
-20
lines changed

4 files changed

+95
-20
lines changed

app/src/processing/app/Sketch.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -1613,23 +1613,41 @@ public void setCompilingProgress(int percent) {
16131613

16141614

16151615
protected void size(PreferencesMap prefs) throws RunnerException {
1616-
long size = 0;
1617-
String maxsizeString = prefs.get("upload.maximum_size");
1618-
if (maxsizeString == null)
1616+
long textSize = 0;
1617+
long dataSize = 0;
1618+
String maxTextSizeString = prefs.get("upload.maximum_size");
1619+
String maxDataSizeString = prefs.get("upload.maximum_data_size");
1620+
if (maxTextSizeString == null)
16191621
return;
1620-
long maxsize = Integer.parseInt(maxsizeString);
1622+
long maxTextSize = Integer.parseInt(maxTextSizeString);
1623+
long maxDataSize = -1;
1624+
if(maxDataSizeString != null)
1625+
maxDataSize = Integer.parseInt(maxDataSizeString);
16211626
Sizer sizer = new Sizer(prefs);
16221627
try {
1623-
size = sizer.computeSize();
1628+
long[] sizes = sizer.computeSize();
1629+
textSize = sizes[0];
1630+
dataSize = sizes[1];
16241631
System.out.println(I18n
16251632
.format(_("Binary sketch size: {0} bytes (of a {1} byte maximum) - {2}% used"),
1626-
size, maxsize, size * 100 / maxsize));
1633+
textSize, maxTextSize, textSize * 100 / maxTextSize));
1634+
if(maxDataSize > 0) {
1635+
System.out.println(I18n
1636+
.format(_("Memory usage: {0} bytes (of a {1} byte maximum) - {2}% used"),
1637+
dataSize, maxDataSize, dataSize * 100 / maxDataSize));
1638+
} else {
1639+
System.out.println(I18n
1640+
.format(_("Memory usage: {0} bytes"),
1641+
dataSize));
1642+
}
16271643
} catch (RunnerException e) {
16281644
System.err.println(I18n.format(_("Couldn't determine program size: {0}"),
16291645
e.getMessage()));
16301646
}
16311647

1632-
if (size > maxsize)
1648+
/* At least 10% of RAM should be reserved for stack/heap usage */
1649+
if (textSize > maxTextSize ||
1650+
(maxDataSize > 0 && dataSize > maxDataSize*9/10))
16331651
throw new RunnerException(
16341652
_("Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it."));
16351653
}

app/src/processing/app/debug/Sizer.java

+44-11
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,40 @@
3333
import processing.app.helpers.StringReplacer;
3434

3535
public class Sizer implements MessageConsumer {
36-
private long size;
36+
private long textSize;
37+
private long dataSize;
38+
private long eepromSize;
3739
private RunnerException exception;
3840
private PreferencesMap prefs;
3941
private String firstLine;
40-
private Pattern pattern;
42+
private Pattern textPattern;
43+
private Pattern dataPattern;
44+
private Pattern eepromPattern;
4145

4246
public Sizer(PreferencesMap _prefs) {
4347
prefs = _prefs;
44-
pattern = Pattern.compile(prefs.get("recipe.size.regex"));
48+
textPattern = Pattern.compile(prefs.get("recipe.size.regex"));
49+
dataPattern = null;
50+
String pref = prefs.get("recipe.size.regex.data");
51+
if(pref != null)
52+
dataPattern = Pattern.compile(pref);
53+
eepromPattern = null;
54+
pref = prefs.get("recipe.size.regex.eeprom");
55+
if(pref != null)
56+
eepromPattern = Pattern.compile(pref);
4557
}
4658

47-
public long computeSize() throws RunnerException {
59+
public long[] computeSize() throws RunnerException {
4860

4961
int r = 0;
5062
try {
5163
String pattern = prefs.get("recipe.size.pattern");
5264
String cmd[] = StringReplacer.formatAndSplit(pattern, prefs, true);
5365

5466
exception = null;
55-
size = -1;
67+
textSize = -1;
68+
dataSize = -1;
69+
eepromSize = -1;
5670
Process process = Runtime.getRuntime().exec(cmd);
5771
MessageSiphon in = new MessageSiphon(process.getInputStream(), this);
5872
MessageSiphon err = new MessageSiphon(process.getErrorStream(), this);
@@ -77,17 +91,36 @@ public long computeSize() throws RunnerException {
7791
if (exception != null)
7892
throw exception;
7993

80-
if (size == -1)
94+
if (textSize == -1)
8195
throw new RunnerException(firstLine);
8296

83-
return size;
97+
return new long[] { textSize, dataSize, eepromSize };
8498
}
8599

86100
public void message(String s) {
87101
if (firstLine == null)
88102
firstLine = s;
89-
Matcher matcher = pattern.matcher(s.trim());
90-
if (matcher.matches())
91-
size = Long.parseLong(matcher.group(1));
103+
Matcher textMatcher = textPattern.matcher(s.trim());
104+
if (textMatcher.matches()) {
105+
if (textSize < 0)
106+
textSize = 0;
107+
textSize += Long.parseLong(textMatcher.group(1));
108+
}
109+
if(dataPattern != null) {
110+
Matcher dataMatcher = dataPattern.matcher(s.trim());
111+
if (dataMatcher.matches()) {
112+
if (dataSize < 0)
113+
dataSize = 0;
114+
dataSize += Long.parseLong(dataMatcher.group(1));
115+
}
116+
}
117+
if(eepromPattern != null) {
118+
Matcher eepromMatcher = eepromPattern.matcher(s.trim());
119+
if (eepromMatcher.matches()) {
120+
if (eepromSize < 0)
121+
eepromSize = 0;
122+
eepromSize += Long.parseLong(eepromMatcher.group(1));
123+
}
124+
}
92125
}
93-
}
126+
}

hardware/arduino/avr/boards.txt

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ uno.name=Arduino Uno
88
uno.upload.tool=avrdude
99
uno.upload.protocol=arduino
1010
uno.upload.maximum_size=32256
11+
uno.upload.maximum_data_size=2048
1112
uno.upload.speed=115200
1213

1314
uno.bootloader.tool=avrdude
@@ -46,6 +47,7 @@ atmega328diecimila.build.variant=standard
4647
atmega328diecimila.menu.cpu.atmega328=ATmega328
4748

4849
atmega328diecimila.menu.cpu.atmega328.upload.maximum_size=30720
50+
atmega328diecimila.menu.cpu.atmega328.upload.maximum_data_size=2048
4951
atmega328diecimila.menu.cpu.atmega328.upload.speed=57600
5052

5153
atmega328diecimila.menu.cpu.atmega328.bootloader.high_fuses=0xDA
@@ -59,6 +61,7 @@ atmega328diecimila.menu.cpu.atmega328.build.mcu=atmega328p
5961
atmega328diecimila.menu.cpu.atmega168=ATmega168
6062

6163
atmega328diecimila.menu.cpu.atmega168.upload.maximum_size=14336
64+
atmega328diecimila.menu.cpu.atmega168.upload.maximum_data_size=1024
6265
atmega328diecimila.menu.cpu.atmega168.upload.speed=19200
6366

6467
atmega328diecimila.menu.cpu.atmega168.bootloader.high_fuses=0xdd
@@ -88,6 +91,7 @@ nano.build.variant=eightanaloginputs
8891
nano.menu.cpu.atmega328=ATmega328
8992

9093
nano.menu.cpu.atmega328.upload.maximum_size=30720
94+
nano.menu.cpu.atmega328.upload.maximum_data_size=2048
9195
nano.menu.cpu.atmega328.upload.speed=57600
9296

9397
nano.menu.cpu.atmega328.bootloader.low_fuses=0xFF
@@ -102,6 +106,7 @@ menu.cpu.nano.atmega328.build.mcu=atmega328p
102106
nano.menu.cpu.atmega168=ATmega168
103107

104108
nano.menu.cpu.atmega168.upload.maximum_size=14336
109+
nano.menu.cpu.atmega168.upload.maximum_data_size=1024
105110
nano.menu.cpu.atmega168.upload.speed=19200
106111

107112
nano.menu.cpu.atmega168.bootloader.low_fuses=0xff
@@ -165,6 +170,7 @@ leonardo.name=Arduino Leonardo
165170
leonardo.upload.tool=avrdude
166171
leonardo.upload.protocol=avr109
167172
leonardo.upload.maximum_size=28672
173+
leonardo.upload.maximum_data_size=2560
168174
leonardo.upload.speed=57600
169175
leonardo.upload.disable_flushing=true
170176
leonardo.upload.use_1200bps_touch=true
@@ -193,6 +199,7 @@ micro.name=Arduino Micro
193199
micro.upload.tool=avrdude
194200
micro.upload.protocol=avr109
195201
micro.upload.maximum_size=28672
202+
micro.upload.maximum_data_size=2560
196203
micro.upload.speed=57600
197204
micro.upload.disable_flushing=true
198205
micro.upload.use_1200bps_touch=true
@@ -221,6 +228,7 @@ esplora.name=Arduino Esplora
221228
esplora.upload.tool=avrdude
222229
esplora.upload.protocol=avr109
223230
esplora.upload.maximum_size=28672
231+
esplora.upload.maximum_data_size=2560
224232
esplora.upload.speed=57600
225233
esplora.upload.disable_flushing=true
226234
esplora.upload.use_1200bps_touch=true
@@ -265,6 +273,7 @@ mini.build.variant=eightanaloginputs
265273
mini.menu.cpu.atmega328=ATmega328
266274

267275
mini.menu.cpu.atmega328.upload.maximum_size=28672
276+
mini.menu.cpu.atmega328.upload.maximum_data_size=2048
268277
mini.menu.cpu.atmega328.upload.speed=115200
269278

270279
mini.menu.cpu.atmega328.bootloader.high_fuses=0xd8
@@ -278,6 +287,7 @@ mini.menu.cpu.atmega328.build.mcu=atmega328p
278287
mini.menu.cpu.atmega168=ATmega168
279288

280289
mini.menu.cpu.atmega168.upload.maximum_size=14336
290+
mini.menu.cpu.atmega168.upload.maximum_data_size=1024
281291
mini.menu.cpu.atmega168.upload.speed=19200
282292

283293
mini.menu.cpu.atmega168.bootloader.high_fuses=0xdd
@@ -293,6 +303,7 @@ ethernet.name=Arduino Ethernet
293303
ethernet.upload.tool=avrdude
294304
ethernet.upload.protocol=arduino
295305
ethernet.upload.maximum_size=32256
306+
ethernet.upload.maximum_data_size=2048
296307
ethernet.upload.speed=115200
297308

298309
ethernet.bootloader.tool=avrdude
@@ -316,6 +327,7 @@ fio.name=Arduino Fio
316327
fio.upload.tool=avrdude
317328
fio.upload.protocol=arduino
318329
fio.upload.maximum_size=30720
330+
fio.upload.maximum_data_size=2048
319331
fio.upload.speed=57600
320332

321333
fio.bootloader.tool=avrdude
@@ -355,6 +367,7 @@ bt.build.variant=eightanaloginputs
355367
## -----------------------
356368
bt.menu.cpu.atmega328=ATmega328
357369
bt.menu.cpu.atmega328.upload.maximum_size=28672
370+
bt.menu.cpu.atmega328.upload.maximum_data_size=2048
358371

359372
bt.menu.cpu.atmega328.bootloader.high_fuses=0xd8
360373
bt.menu.cpu.atmega328.bootloader.extended_fuses=0x05
@@ -366,6 +379,7 @@ bt.menu.cpu.atmega328.build.mcu=atmega328p
366379
## -----------------------
367380
bt.menu.cpu.atmega168=ATmega168
368381
bt.menu.cpu.atmega168.upload.maximum_size=14336
382+
bt.menu.cpu.atmega168.upload.maximum_data_size=1024
369383

370384
bt.menu.cpu.atmega168.bootloader.high_fuses=0xdd
371385
bt.menu.cpu.atmega168.bootloader.extended_fuses=0x00
@@ -380,6 +394,7 @@ LilyPadUSB.name=LilyPad Arduino USB
380394
LilyPadUSB.upload.tool=avrdude
381395
LilyPadUSB.upload.protocol=avr109
382396
LilyPadUSB.upload.maximum_size=28672
397+
LilyPadUSB.upload.maximum_data_size=2560
383398
LilyPadUSB.upload.speed=57600
384399
LilyPadUSB.upload.disable_flushing=true
385400
LilyPadUSB.upload.use_1200bps_touch=true
@@ -423,6 +438,7 @@ lilypad.build.variant=standard
423438
lilypad.menu.cpu.atmega328=ATmega328
424439

425440
lilypad.menu.cpu.atmega328.upload.maximum_size=30720
441+
lilypad.menu.cpu.atmega328.upload.maximum_data_size=2048
426442
lilypad.menu.cpu.atmega328.upload.speed=57600
427443

428444
lilypad.menu.cpu.atmega328.bootloader.low_fuses=0xFF
@@ -437,6 +453,7 @@ lilypad.menu.cpu.atmega328.build.mcu=atmega328p
437453
lilypad.menu.cpu.atmega168=ATmega168
438454

439455
lilypad.menu.cpu.atmega168.upload.maximum_size=14336
456+
lilypad.menu.cpu.atmega168.upload.maximum_data_size=1024
440457
lilypad.menu.cpu.atmega168.upload.speed=19200
441458

442459
lilypad.menu.cpu.atmega168.bootloader.low_fuses=0xe2
@@ -466,6 +483,7 @@ pro.build.variant=standard
466483
pro.menu.cpu.16MHzatmega328=ATmega328 (5V, 16 MHz)
467484

468485
pro.menu.cpu.16MHzatmega328.upload.maximum_size=30720
486+
pro.menu.cpu.16MHzatmega328.upload.maximum_data_size=2048
469487
pro.menu.cpu.16MHzatmega328.upload.speed=57600
470488

471489
pro.menu.cpu.16MHzatmega328.bootloader.low_fuses=0xFF
@@ -481,6 +499,7 @@ pro.menu.cpu.16MHzatmega328.build.f_cpu=16000000L
481499
pro.menu.cpu.8MHzatmega328=ATmega328 (3.3V, 8 MHz)
482500

483501
pro.menu.cpu.8MHzatmega328.upload.maximum_size=30720
502+
pro.menu.cpu.8MHzatmega328.upload.maximum_data_size=2048
484503
pro.menu.cpu.8MHzatmega328.upload.speed=57600
485504

486505
pro.menu.cpu.8MHzatmega328.bootloader.low_fuses=0xFF
@@ -496,6 +515,7 @@ pro.menu.cpu.8MHzatmega328.build.f_cpu=8000000L
496515
pro.menu.cpu.16MHzatmega168=ATmega168 (5V, 16 MHz)
497516

498517
pro.menu.cpu.16MHzatmega168.upload.maximum_size=14336
518+
pro.menu.cpu.16MHzatmega168.upload.maximum_data_size=1024
499519
pro.menu.cpu.16MHzatmega168.upload.speed=19200
500520

501521
pro.menu.cpu.16MHzatmega168.bootloader.low_fuses=0xff
@@ -511,6 +531,7 @@ pro.menu.cpu.16MHzatmega168.build.f_cpu=16000000L
511531
pro.menu.cpu.8MHzatmega168=ATmega168 (3.3V, 8 MHz)
512532

513533
pro.menu.cpu.8MHzatmega168.upload.maximum_size=14336
534+
pro.menu.cpu.8MHzatmega168.upload.maximum_data_size=1024
514535
pro.menu.cpu.8MHzatmega168.upload.speed=19200
515536

516537
pro.menu.cpu.8MHzatmega168.bootloader.low_fuses=0xc6
@@ -544,6 +565,7 @@ atmegang.build.variant=standard
544565
atmegang.menu.cpu.atmega168=ATmega168
545566

546567
atmegang.menu.cpu.atmega168.upload.maximum_size=14336
568+
atmegang.menu.cpu.atmega168.upload.maximum_data_size=1024
547569

548570
atmegang.menu.cpu.atmega168.bootloader.low_fuses=0xff
549571
atmegang.menu.cpu.atmega168.bootloader.high_fuses=0xdd

hardware/arduino/avr/platform.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ recipe.objcopy.eep.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.obj
5353
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
5454

5555
## Compute size
56-
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.hex"
57-
recipe.size.regex=Total\s+([0-9]+).*
56+
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
57+
recipe.size.regex=^(?:\.text|\.data|\.bootloader)\s+([0-9]+).*
58+
recipe.size.regex.data=^(?:\.data|\.bss|\.noinit)\s+([0-9]+).*
59+
recipe.size.regex.eeprom=^(?:\.eeprom)\s+([0-9]+).*
5860

5961

6062
# AVR Uploader/Programmers tools

0 commit comments

Comments
 (0)