chipKIT® Development Platform

Inspired by Arduino™

How to Measure Available Memory?

Created Sat, 24 Dec 2011 15:50:14 +0000 by avenue33


avenue33

Sat, 24 Dec 2011 15:50:14 +0000

How to measure the available memory?

The sketches for Arduino are based on AVR-controller specificities and are not compatible with chipKIT pic32 controllers.

Thank you for your help,


Jacob Christ

Tue, 27 Dec 2011 13:19:28 +0000

Please clarify your question. It looks like randomly chosen strings of text from other threads.

Jacob


avenue33

Tue, 27 Dec 2011 15:08:53 +0000

No random text, just a real question.

Arduino offers two routine to check the available memory: see :arrow: [url]http://www.arduino.cc/playground/Code/AvailableMemory[/url].

uint8_t * heapptr, * stackptr;
void check_mem() {
  stackptr = (uint8_t *)malloc(4);          // use stackptr temporarily
  heapptr = stackptr;                     // save value of heap pointer
  free(stackptr);      // free up the memory again (sets stackptr to 0)
  stackptr =  (uint8_t *)(SP);           // save value of stack pointer
}

Trying to compile the sketch raises errors because (SP) is AVR-based.

Ok, so now you have the question in its full glory, could you help me? I would greatly appreciate.

Thank you and best regards


Jacob Christ

Tue, 27 Dec 2011 17:39:46 +0000

Ah, much better and I understand specifically what your asking now. Sorry, there are a lot of robots that post to this message board.

I haven't used these functions so someone else will need to chime in.

Jacob


avenue33

Tue, 27 Dec 2011 17:53:28 +0000

Sorry, there are a lot of robots that post to this message board.

No offence. I wasn't aware robots could post such a message.

This is the Turing Test reversed:

Sad times ;)


username

Mon, 16 Jan 2012 22:41:47 +0000

We could still us an answer... :)

I'm running into 'out of memory' type conditions on a MEGA, and there hasn't even been that much allocation... :(


avenue33

Mon, 16 Jan 2012 23:36:14 +0000

So do I, waiting for an answer!


whoover

Mon, 06 Feb 2012 16:07:33 +0000

Maybe this will help?


KeithV

Mon, 06 Feb 2012 16:36:37 +0000

If you are a MPLAB users this is what you can do.

You can build your sketch in MPIDE as usual, but do a Shift compiler. At the end you will see an .elf file. Select and copy the .elf path into your clipboard.

Then start MPLAB,

  1. under configure->select device (then select the processor you are using).
  2. under File->import (paste your .elf file and import it)
  3. select view->memory usage guage

The memory usage guage will come up and tell you how much ROM and RAM is being used by your sketch; as loaded. Becareful, it may give it to you in "words" not bytes. Rt mouse over the usage guage to toggle between words and bytes.

As for at runtime, here is a sketch where I change the defualt 2K heap size to 0x5000. It also shows you the top of the heap and how much free memory you have between the top of the heap and the bottom of the current stack point.

#define CHANGE_HEAP_SIZE(size) asm volatile ("\t.globl _min_heap_size\n\t.equ _min_heap_size, " #size "\n")

CHANGE_HEAP_SIZE(0x5000);

extern attribute((section("linker_defined"))) char _heap; extern attribute((section("linker_defined"))) char _min_heap_size;

void setup() {

byte * pTopHeap = (byte *) &_heap + (unsigned int) &_min_heap_size;
// initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.print ((unsigned int)&_heap, HEX); Serial.print ('\n'); Serial.print ((unsigned int)&_min_heap_size, HEX); Serial.print ('\n'); Serial.print("Bottom of stack: "); Serial.println((unsigned int) &pTopHeap, HEX); Serial.print("top of heap: "); Serial.println((unsigned int) pTopHeap, HEX); }

void loop() { digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off delay(1000); // wait for a second }

Hope this is what you were looking for


KeithV

Mon, 06 Feb 2012 16:41:42 +0000

I'm running into 'out of memory' type conditions on a MEGA, and there hasn't even been that much allocation... :(

This is because by default there is only a 2K heap defined, even though you have tons of available memory; the heap is limited to 2K. The stack will grow down into the heap, there are no checks when the stack gets that far down. If you know what you max stack usage is, you can then reset you heap to be as large as available memory with the sketch I just gave you.