您可以使用它。它会让您了解自己的位置:
Serial.print(availableMemory());
// free RAM check for debugging. SRAM for ATmega328p = 2048Kb.
int availableMemory() {
// Use 1024 with ATmega168
int size = 2048;
byte *buf;
while ((buf = (byte *) malloc(--size)) == NULL);
free(buf);
return size;
}
tron使用
memoryfree在
arduinoPlayground上,有有关此问题的详细信息,这就是我发现我一直在使用的方法:
// MemoryFree library based on code posted here:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/15
//
// Extended by Matthew Murdoch to include walking of the free list.
#ifndef MEMORY_FREE_H
#define MEMORY_FREE_H
#ifdef __cplusplus
extern "C" {
#endif
int freeMemory();
#ifdef __cplusplus
}
#endif
#endif
memoryfree.cpp:
#include <MemoryFree.h>
// On Arduino Duemilanove with ATmega328:
//
// Reported free memory with str commented out:
// 1824 bytes
//
// Reported free memory with str and Serial.println(str) uncommented:
// 1810
//
// Difference: 14 bytes (13 ascii chars + null terminator)
// 14-bytes string
//char str[] = "Hello, world!";
void setup() {
Serial.begin(115200);
}
void loop() {
//Serial.println(str);
Serial.print("freeMemory()=");
Serial.println(freeMemory());
delay(1000);
}
建立Esben的答案,我写了此优化版本:
int biggestMemoryBlock(uint16_t min,uint16_t max)
{
if (min==max-1)
return min;
int size=max;
int lastSize=size;
byte *buf;
while ((buf = (byte *) malloc(size)) == NULL)
{
lastSize=size;
size-=(max-min)/2;
};
free(buf);
return biggestMemoryBlock(size,lastSize);
};
int biggestMemoryBlock()
{
return biggestMemoryBlock(0,4096);
}