@@ -74,6 +74,9 @@ enum class WiFiStatsCountType
74
74
kWiFiOverrunCount
75
75
};
76
76
77
+ // Static variable to store the maximum heap size
78
+ static size_t maxHeapHighWatermark = 0 ;
79
+
77
80
CHIP_ERROR GetEthernetStatsCount (EthernetStatsCountType type, uint64_t & count)
78
81
{
79
82
CHIP_ERROR err = CHIP_ERROR_READ_FAILED;
@@ -244,6 +247,11 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeap
244
247
// the current running program.
245
248
currentHeapUsed = mallocInfo.uordblks ;
246
249
250
+ // Update the maximum heap high watermark if the current heap usage exceeds it.
251
+ if (currentHeapUsed > maxHeapHighWatermark)
252
+ {
253
+ maxHeapHighWatermark = currentHeapUsed;
254
+ }
247
255
return CHIP_NO_ERROR;
248
256
#endif
249
257
}
@@ -260,9 +268,15 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu
260
268
// has been used by the Node.
261
269
// On Linux, since it uses virtual memory, whereby a page of memory could be copied to
262
270
// the hard disk, called swap space, and free up that page of memory. So it is impossible
263
- // to know accurately peak physical memory it use. We just return the current heap memory
264
- // being used by the current running program.
265
- currentHeapHighWatermark = mallocInfo.uordblks ;
271
+ // to know accurately peak physical memory it use.
272
+ // Update the maximum heap high watermark if the current heap usage exceeds it.
273
+ if (mallocInfo.uordblks > static_cast <int >(maxHeapHighWatermark))
274
+ {
275
+ maxHeapHighWatermark = mallocInfo.uordblks ;
276
+ }
277
+
278
+ // Set the current heap high watermark.
279
+ currentHeapHighWatermark = maxHeapHighWatermark;
266
280
267
281
return CHIP_NO_ERROR;
268
282
#endif
@@ -275,6 +289,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::ResetWatermarks()
275
289
276
290
// On Linux, the write operation is non-op since we always rely on the mallinfo system
277
291
// function to get the current heap memory.
292
+ struct mallinfo mallocInfo = mallinfo ();
293
+ maxHeapHighWatermark = mallocInfo.uordblks ;
278
294
279
295
return CHIP_NO_ERROR;
280
296
}
0 commit comments