我正在使用Arduino進行一個簡單的項目。最近,我必須將我的一個變量轉換爲long而不是int,爲了保持簡單,我只是移動了與它交互的所有數字(所以我不必擔心交叉類型比較和數學)。這看起來很浪費,但它對我來說只是一個時鐘,我不在乎。檢查Arduino中的內存佔用情況
但是,它讓我想知道我使用了多少內存。我懷疑這是一個問題,但我意識到我不知道任何方法來檢查。
那麼,有沒有辦法來檢查一個Arduino使用的內存量?
理想情況下,我想打印出串行連接上可用的當前內存/總數。
我正在使用Arduino進行一個簡單的項目。最近,我必須將我的一個變量轉換爲long而不是int,爲了保持簡單,我只是移動了與它交互的所有數字(所以我不必擔心交叉類型比較和數學)。這看起來很浪費,但它對我來說只是一個時鐘,我不在乎。檢查Arduino中的內存佔用情況
但是,它讓我想知道我使用了多少內存。我懷疑這是一個問題,但我意識到我不知道任何方法來檢查。
那麼,有沒有辦法來檢查一個Arduino使用的內存量?
理想情況下,我想打印出串行連接上可用的當前內存/總數。
您可以使用它。它會給你有關你在哪裏的想法:
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;
}
這隻測量堆使用情況(忽略堆棧和全局變量),並且如果堆發生碎片化將會失敗。 – Bill 2013-01-05 19:07:41
嘗試使用MemoryFree。這個庫對我來說工作得很好。
有關於這比在Arduino Playground的詳細信息,這就是我發現我一直在使用方法:
MemoryFree.h:
// 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
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include
#endif
extern unsigned int __heap_start;
extern void *__brkval;
/*
* The free list structure as maintained by the
* avr-libc memory allocation routines.
*/
struct __freelist {
size_t sz;
struct __freelist *nx;
};
/* The head of the free list structure */
extern struct __freelist *__flp;
#include "MemoryFree.h";
/* Calculates the size of the free list */
int freeListSize() {
struct __freelist* current;
int total = 0;
for (current = __flp; current; current = current->nx) {
total += 2; /* Add two bytes for the memory block's header */
total += (int) current->sz;
}
return total;
}
int freeMemory() {
int free_memory;
if ((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__heap_start);
} else {
free_memory = ((int)&free_memory) - ((int)__brkval);
free_memory += freeListSize();
}
return free_memory;
}
#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);
}
在Arduino的烏諾它只需1毫秒以下,而不是13-20毫秒內的原有功能。該常數必須至少是板上的總內存(以字節爲單位)。
給http://electronics.stackexchange.com/一個嘗試,雖然這是編程相關的,但我認爲你會得到更好的迴應,社區誰做更多的工作,專門與微控制器,如Arduino。 – JYelton 2011-12-27 20:55:55
謝謝,如果我沒有得到我所需要的,我會嘗試。 – MrGlass 2011-12-27 21:04:38
好吧,讓我們看看他們說什麼http://electronics.stackexchange.com/q/24269/7247 – MrGlass 2011-12-28 15:55:26