Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ESP::getHeapSize() and friends return INTERNAL memory, not taking SPIMEM into account #12011

vvb333007 started this conversation in General
Discussion options

Board

any

Device Description

esp32s3 dev module

Hardware Configuration

no connections except UART0 and power

Version

latest stable Release (if not listed below)

Type

Bug

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80

PSRAM enabled

yes

Upload speed

115200

Description

class EspClass of Arduino Core (Esp.cpp) has getters for heap size/free size/etc, which for some reason return values of internal memory only. In particular, on my esp32s3 these return values below 300kb, while malloc() can allocate up to 8MB.

Proposed fix is extremely simple:

Replace MALLOC_CAP_INTERNAL with MALLOC_CAP_DEFAULT in functions:

[code]
uint32_t EspClass::getHeapSize(void) {
return heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
}

uint32_t EspClass::getFreeHeap(void) {
return heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
}

uint32_t EspClass::getMinFreeHeap(void) {
return heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
}

uint32_t EspClass::getMaxAllocHeap(void) {
return heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
}
[/code]

It would be better if we can get real sizes with ::getFreeHeap(). For rare cases when we need to know INTERNAL memory sizes, we can use specific queries, like uint32_t EspClass::getSRAMSize(void)

Sketch

uint32_t size1 = ESP.getHeapSize();
uint32_t size2 = heap_caps_get_total_size(MALLOC_CAP_DEFAULT);
if (size2 != size1)
 Serial.printf("size1=%lu, size2=%lu\r\n",size1,size2)

Debug Message

none

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

There are different reasons why this is so:

  • Historically this API shows only internal memory stats
  • If you have available PSRAM memory, but no internal one, you are in trouble.
  • There are getters for PSRAM stats
  • PSRAM used to not be available through malloc
You must be logged in to vote
1 reply
Comment options

But now SPIRAM is available to malloc, so may be it is good idea to update these getters?

How I see this: user calls getter function to query memory size and then calls malloc() or new(). In this case it is safe to replace _INTERNAL to _DEFAULT - getter will return values larger than before so it should not break any existing code.

It is also seems right to hide all these internal/spi/etc things from the class user. If it is heap - it is heap and we should return actual size. For those rare cases when user requires INTERNAL memory (for DMA buffers for example) - he/she will likely use heap_caps_malloc().

Since SPIRAM is under malloc()/MALLOC_CAPS_DEFAULT now, may be it is worth to remove ...Psram() functions entirely, from class ESP: it will make programming more transparent. Users don't have to think about SPI/INTERNAL RAM at all. They just call getHeapSize() or getHeapFreeSize() and then do (or don't) allocations.

Just an opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting triage Issue is waiting for triage
Converted from issue

This discussion was converted from issue #12010 on November 11, 2025 09:20.

AltStyle によって変換されたページ (->オリジナル) /