arduino / arduino/ArduinoCore-renesas
Excessive use of BSS by certain static objects
- Dominant language
- C
- Stars
- 193
- Forks
- 112
- PR merge metrics
- No merged PRs in 30d
Description
**Core version**: 1.1.0
**Variant**: UNO R4 WIFI
## Let's take this simple sketch and built it for UNO R4 Wi-Fi target:
```
extern "C" void * _sbrk (int);
static uint32_t RA4M1_getFreeHeap()
{
char top;
return &top - reinterpret_cast(_sbrk(0));
}
void setup()
{
Serial.begin(38400);
for (int i=0; i < 20; i++) {if (Serial) break; else delay(100);}
Serial.print("Free Heap = "); Serial.println(RA4M1_getFreeHeap());
Serial.flush();
}
void loop() { }
```
These are the static objects of the sketch that consume most of RAM space:
```
$ arm-none-eabi-nm -n --size-sort sketch.elf | grep ' B ' | tail -12
00000010 B g_external_irq1_ctrl
00000014 B SerialUSB
00000018 B _ZN7arduino11IN6ADDR_ANYE
00000018 B _ZN7arduino11INADDR_NONEE
00000028 B _ZN4UART7g_uartsE
00000038 B _ZN4Tone10tone_timerE
0000003c B _dac12
00000040 B g_bsp_group_irq_sources
00000080 B gp_renesas_isr_context
000004ac B _UART1_
000004ac B _UART2_
000004ac B _UART3_
```
2 of 3 UARTs are not in use by the sketch itself but they consume 1+ KByte each.
When we execute the sketch - it makes a report of free heap available:
```
Free Heap = 25635
```
Let's alter the sketch by adding `#include ` line:
```
#include
extern "C" void * _sbrk (int);
static uint32_t RA4M1_getFreeHeap()
{
char top;
return &top - reinterpret_cast(_sbrk(0));
}
void setup()
{
Serial.begin(38400);
for (int i=0; i < 20; i++) {if (Serial) break; else delay(100);}
Serial.print("Free Heap = "); Serial.println(RA4M1_getFreeHeap());
Serial.flush();
}
void loop() { }
```
```
$ arm-none-eabi-nm -n --size-sort sketch.elf | grep ' B ' | tail -12
00000018 B _ZN7arduino11INADDR_NONEE
00000028 B _ZN4UART7g_uartsE
00000028 B _ZN7TwoWire10g_I2CWiresE
00000038 B _ZN4Tone10tone_timerE
0000003c B _dac12
00000040 B g_bsp_group_irq_sources
00000080 B gp_renesas_isr_context
00000448 B Wire
00000448 B Wire1
000004ac B _UART1_
000004ac B _UART2_
000004ac B _UART3_
```
We can see now that 2 more (`Wire` and `Wire1`) objects consume 1+ Kbyte of RAM each. Both of these objects are actually not in use by the sketch.
Let's execute the sketch as well:
```
Free Heap = 23395
```
We can see that this 'hello world' kind of sketch consumes 9 KBytes out of 32 KBytes RAM available in the Renesas RA4M1 MCU.
## Let's build and execute the sketch on **Arduino Zero/M0** (SAMD21) target:
There are the results when `Wire.h` is not included:
```
00000004 B _usbSetInterface
00000008 B sercom0
00000008 B sercom1
00000008 B sercom2
00000008 B sercom3
00000008 B sercom4
00000008 B sercom5
00000028 B EndPoints
0000003c B SerialUSB
00000100 B _pack_buffer
00000104 B usbd
00000244 B Serial1
Free Heap = 30063
```
And this one is taken with `Wire.h`:
```
00000008 B sercom0
00000008 B sercom1
00000008 B sercom2
00000008 B sercom3
00000008 B sercom4
00000008 B sercom5
00000028 B EndPoints
0000003c B SerialUSB
00000100 B _pack_buffer
00000104 B usbd
0000023c B Wire
00000244 B Serial1
Free Heap = 29487
```
# Summary
Both RA4M1 and SAMD21 have 32 Kbytes of RAM available.
However, the Arduino Core for Renesas MCUs consumes a lot more RAM space for unused static objects rather than Arduino Core for SAMD does.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the supplied sketches and compare the arm-none-eabi-nm BSS listings for the UNO R4 WiFi and Arduino Zero/M0 targets, first without and then with Wire.h. Trace the static UART and Wire objects reported there and determine why unused instances are retained. Done means the unused allocations are no longer consuming RAM while the shown sketches still build and run correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100