Skip to content

ESP32 Memory Capability Allocation Wrong Type

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 Memory Capability Allocation Wrong Type. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 heap_caps_malloc() returns NULL or allocates from wrong memory type.

Quick Fix

Wrong

void* buf = malloc(10000);  // May use PSRAM or DRAM
Memory allocated from wrong pool. Performance issues or crashes.
// Internal DRAM (fast, limited):
void* dram = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
// PSRAM (large, slower):
void* psram = heap_caps_malloc(65536, MALLOC_CAP_SPIRAM);
// DMA-safe memory:
void* dma = heap_caps_malloc(512, MALLOC_CAP_DMA);
// Internal fast memory:
void* fast = heap_caps_malloc(256, MALLOC_CAP_INTERNAL);
Serial.printf("DRAM: %p, PSRAM: %p, DMA: %p, FAST: %p\n",
  dram, psram, dma, fast);
DRAM: 0x3FFB0000, PSRAM: 0x3F800000, DMA: 0x3FFAE000, FAST: 0x3FFB0000

Prevention

Use heap_caps_malloc() for specific memory types. MALLOC_CAP_8BIT = DRAM. MALLOC_CAP_SPIRAM = PSRAM. MALLOC_CAP_INTERNAL = internal SRAM. Always check return for NULL. Use heap_caps_free() to free.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What memory types does ESP32 support?

DRAM (8-bit data), IRAM (code), D/IRAM (shared), DMA (contiguous), SPIRAM (external PSRAM), RTC_SLOW_MEM (sleep persistent).

How much internal DRAM is available?

ESP32 has ~320KB internal DRAM. ~160KB used by system. ~160KB free for applications.

Should I prefer DRAM or PSRAM?

DRAM: fast but limited (160KB). PSRAM: large (up to 8MB) but slower. Use DRAM for performance-critical, PSRAM for large buffers.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro