Skip to content

Excessive use of BSS by certain static objects #283

Open
@lyusupov

Description

@lyusupov

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<char*>(_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 <Wire.h> line:

#include <Wire.h>

extern "C" void * _sbrk   (int);

static uint32_t RA4M1_getFreeHeap()
{
  char top;
  return &top - reinterpret_cast<char*>(_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions