-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
HeapStat.h
146 lines (114 loc) · 2.57 KB
/
HeapStat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/** Memory usage info for debugging purpose
#include "HeapStat.h"
HeapStat heapInfo;
void setup()
{
Serial.begin(115200);
}
void loop()
{
// Other codes here
// Collect memory info
heapInfo.collect();
// Print memory info
heapInfo.print();
}
*/
#ifndef HEAP_STAT_H
#define HEAP_STAT_H
#if defined(ESP8266) || defined(ESP32)
#ifndef MB_ARDUINO_ESP
#define MB_ARDUINO_ESP
#endif
#endif
#if defined(ARDUINO_ARCH_RP2040)
#if defined(ARDUINO_NANO_RP2040_CONNECT)
#ifndef MB_ARDUINO_NANO_RP2040_CONNECT
#define MB_ARDUINO_NANO_RP2040_CONNECT
#endif
#else
#ifndef MB_ARDUINO_PICO
#define MB_ARDUINO_PICO
#endif
#endif
#endif
#include "ESP_Mail_Client.h"
class HeapStat
{
private:
int current_heap = 0;
int first_round_heap = 0;
int min_heap = 0;
int max_heap = 0;
int diff_heap_from_first_round = 0;
int diff_heap_from_current_round = 0;
int _count = 0;
int getFreeHeap()
{
#if defined(MB_ARDUINO_ESP)
return ESP.getFreeHeap();
#elif defined(MB_ARDUINO_PICO)
return rp2040.getFreeHeap();
#else
return 0;
#endif
}
public:
HeapStat() {}
~HeapStat() {}
void reset()
{
current_heap = 0;
first_round_heap = 0;
min_heap = 0;
max_heap = 0;
diff_heap_from_first_round = 0;
diff_heap_from_current_round = 0;
_count = 0;
}
void collect()
{
_count++;
if (max_heap < getFreeHeap())
max_heap = getFreeHeap();
if (min_heap == 0 || min_heap > getFreeHeap())
min_heap = getFreeHeap();
if (first_round_heap == 0)
first_round_heap = getFreeHeap();
if (current_heap > 0)
{
diff_heap_from_first_round = getFreeHeap() - first_round_heap;
diff_heap_from_current_round = getFreeHeap() - current_heap;
}
current_heap = getFreeHeap();
}
int current()
{
return current_heap;
}
int min()
{
return min_heap;
}
int max()
{
return max_heap;
}
int diff1()
{
return diff_heap_from_first_round;
}
int diffN()
{
return diff_heap_from_current_round;
}
int count()
{
return _count;
}
void print()
{
MailClient.printf("#### Heap Info\n#### Current: %d, Min: %d, Max: %d, Diff_1: %d, Diff_%d: %d\n", current(), min(), max(), diff1(), count(), diffN());
}
};
#endif