Skip to content

correct total RAM size, FreeRam.ino malloc failure #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions examples/FreeRam/FreeRam.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
void setup()
{
Serial.begin(115200);
Serial.println(F( "Running " __FILE__ ", Built " __DATE__));
Serial.println(F("Starting state of the memory:"));
Serial.println();

Expand All @@ -17,9 +18,14 @@ void setup()
Serial.println();

FREERAM_PRINT;

//byte *p = new byte[3000];
byte *p = new byte[300]; // Uno (ATmega328) only has 2k RAM

byte *p = new byte[3000];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #6


if(!p) {
Serial.println(F("could not allocate bytes for p[] array!"));
}

Serial.println();
Serial.println();

Expand All @@ -37,6 +43,9 @@ void setup()
Serial.println();

FREERAM_PRINT;

delete p;
p = 0;
}

void loop()
Expand Down
53 changes: 53 additions & 0 deletions examples/GetMemorySize/GetMemorySize.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <MemoryUsage.h>

// Simple example to report memory sizes

void reportAllocation(int numBytes) {

Serial.print(F("Allocating for "));
Serial.print( numBytes );
Serial.print(F(" bytes; "));

byte *p = new byte[numBytes];

if (p) {
Serial.println(F("...success."));
} else {
Serial.println(F("...allocation FAILED"));
}

MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT

Serial.println(F("\ndeleting byte array with delete"));
delete p; // don't want a memory leak!
p = 0; // don't want a dangling/obsolete pointer

MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT
}

void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(F( "Running " __FILE__ ", Built " __DATE__));

Serial.println(F("\nStarting conditions"));
MEMORY_PRINT_TOTALSIZE
MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT

Serial.println(F("\nallocate a byte array with new; too big to fit in RAM?"));
reportAllocation(3000);

Serial.println(F("\nallocate smaller byte array with new (it should fit)"));
reportAllocation(300);

Serial.println(F("\nFinal conditions"));
MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT
}

void loop() {
// User reads output from setup().
}
2 changes: 1 addition & 1 deletion src/MemoryUsage.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ extern uint8_t *__bss_end;
/// Print free ram size on serial console.
#define MEMORY_PRINT_FREERAM { Serial.print(F("Free ram:")); Serial.println((int) SP - (int) (__brkval == 0 ? (int)&__heap_start : (int)__brkval)); }
/// Print total SRAM size on serial console.
#define MEMORY_PRINT_TOTALSIZE { Serial.print(F("SRAM size:")); Serial.println((int) RAMEND - (int) &__data_start); }
#define MEMORY_PRINT_TOTALSIZE { Serial.print(F("SRAM size:")); Serial.println(((int) RAMEND + 1 - (int) &__data_start)); }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #5


/// Displays the 'map' of the current state of the Arduino's SRAM memory on the Serial console.
void SRamDisplay(void);
Expand Down