M5StackCore2 実行中のheap領域のサイズを取得

# heapメモリの使用量を実行中に、チェックしてみる。
クラスインスタンスをnewで生成した時に、freeHeapメモリがどのぐらい減っていくか、チェックしてみました。自作のクラスインスタンスをsizeof(Class_name)で求めると64bytesです。その一つのクラスをnewすると80から120バイトぐらいの領域がfreeHeapから減っていきます。減っていくサイズが64byteより大きく、かつ不定です。
　複数生成し、すべてのクラスインスタンスをdeleteしても、生成する前のfreeHeapサイズより小さくなります。
　newを実行すると、メモリアロケータが、ESP32に複数あるRAMから適切なエリアを選定し、インスタンスに割り付けます。そこでの振る舞いが分かっていない。
　newとdeleteを多数回繰り返したときに、フラグメンテーションが起きている可能性もあるので、もう少し、深い理解が必要そうです。

<参考>
 API Reference » System API » **Heap Memory Allocation**
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/mem_alloc.html

https://engineering.nature.global/entry/esp-idf-v4.3-allocator

https://lang-ship.com/blog/work/esp32-heap-1/

# heapメモリの大きさを取得する主な関数

### **size_t heap_caps_get_total_size(uint32_t caps)**

:::note info
Get the total size of all the regions that have the given capabilities.
This function takes all regions capable of having the given capabilities allocated in them and adds up the total space they have.
Parameters
caps -- Bitwise OR of MALLOC_CAP_* flags indicating the type of memory
Returns
total size in bytes
:::
### **size_t heap_caps_get_free_size(uint32_t caps)**
:::note info
Get the total free size of all the regions that have the given capabilities.
This function takes all regions capable of having the given capabilities allocated in them and adds up the free space they have.
Parameters
caps -- Bitwise OR of MALLOC_CAP_* flags indicating the type of memory
Returns
Amount of free bytes in the regions
:::
:::note warn
Note that because of heap fragmentation it is probably not possible to allocate a single block of memory of this size. Use heap_caps_get_largest_free_block() for this purpose.
:::
### **size_t heap_caps_get_largest_free_block(uint32_t caps)**
:::note info
Get the largest free block of memory able to be allocated with the given capabilities.

Returns the largest value of s for which heap_caps_malloc(s, caps) will succeed.

Parameters
caps -- Bitwise OR of MALLOC_CAP_* flags indicating the type of memory

Returns
Size of the largest free block in bytes.
:::

### **esp_err_t heap_caps_monitor_local_minimum_free_size_start(void)**
:::note info
Start monitoring the value of minimum_free_bytes from the moment this function is called instead of from startup.
Returns
esp_err_t ESP_OK if the function executed properly ESP_FAIL if called when monitoring already active
:::
:::note warn
Note
This allows to detect local lows of the minimum_free_bytes value that wouldn't be detected otherwise.
:::

```C++: macro
Macros
HEAP_IRAM_ATTR
MALLOC_CAP_EXEC
Flags to indicate the capabilities of the various memory systems.

Memory must be able to run executable code

MALLOC_CAP_32BIT
Memory must allow for aligned 32-bit data accesses.

MALLOC_CAP_8BIT
Memory must allow for 8/16/...-bit data accesses.

MALLOC_CAP_DMA
Memory must be able to accessed by DMA.

MALLOC_CAP_PID2
Memory must be mapped to PID2 memory space (PIDs are not currently used)

MALLOC_CAP_PID3
Memory must be mapped to PID3 memory space (PIDs are not currently used)

MALLOC_CAP_PID4
Memory must be mapped to PID4 memory space (PIDs are not currently used)

MALLOC_CAP_PID5
Memory must be mapped to PID5 memory space (PIDs are not currently used)

MALLOC_CAP_PID6
Memory must be mapped to PID6 memory space (PIDs are not currently used)

MALLOC_CAP_PID7
Memory must be mapped to PID7 memory space (PIDs are not currently used)

MALLOC_CAP_SPIRAM
Memory must be in SPI RAM.

MALLOC_CAP_INTERNAL
Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off.

MALLOC_CAP_DEFAULT
Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(), calloc()) call.

MALLOC_CAP_IRAM_8BIT
Memory must be in IRAM and allow unaligned access.

MALLOC_CAP_RETENTION
Memory must be able to accessed by retention DMA.

MALLOC_CAP_RTCRAM
Memory must be in RTC fast memory.

MALLOC_CAP_TCM
Memory must be in TCM memory.

MALLOC_CAP_INVALID
Memory can't be used / list end marker.
```
```C++
void printHeapTotalCapabilitySize()
{
  Serial.println("<total capability size>");
  Serial.printf("MALLOC_CAP_ 8BIT:%d\r",heap_caps_get_total_size(MALLOC_CAP_8BIT));
  Serial.printf("MALLOC_CAP_32BIT:%d\r",heap_caps_get_total_size(MALLOC_CAP_32BIT));
  Serial.printf("MALLOC_CAP_INTERNAL:%d\r",heap_caps_get_total_size(MALLOC_CAP_INTERNAL));
  
}

void setHeapFreeSize(size_t* sz)
{
  sz[0] = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  sz[1] = heap_caps_get_free_size(MALLOC_CAP_32BIT);
}

void printHeapFreeMemorySize(void)
{
  int32_t dif[2];

  dif[0]=oldSize[0]-newSize[0];
  dif[1]=oldSize[1]-newSize[1];

  Serial.println("<heap_caps_get_free_size> execution.");
  Serial.printf("Class size:%dbyte\r",sizeof(GTUnit));
  Serial.printf("                 old(byte) new(byte) ensureSize\r",_s);
  Serial.printf("MALLOC_CAP_ 8BIT: %d   %d     (%dbyte)\r",oldSize[0],newSize[0],dif[0]);
  Serial.printf("MALLOC_CAP_32BIT: %d   %d     (%dbyte)\r",oldSize[1],newSize[1],dif[1]);
  //Serial.printf("MALLOC_CAP_INTERNAL:%d\r",heap_caps_get_total_size(MALLOC_CAP_INTERNAL));
  Serial.println("\r\r");
  //oldSize[0] = newSize[0];
  //oldSize[1] = newSize[1];
  /*Serial.println("<minimum free size>");
  Serial.printf("MALLOC_CAP_ 8BIT:%d\r",heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT));
  Serial.printf("MALLOC_CAP_32BIT:%d\r",heap_caps_get_minimum_free_size(MALLOC_CAP_32BIT));
  Serial.println("<largest free block size>");
  Serial.printf("MALLOC_CAP_ 8BIT:%d\r",heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
  Serial.printf("MALLOC_CAP_32BIT:%d\r",heap_caps_get_largest_free_block(MALLOC_CAP_32BIT));
  */
}
```