Skip to main content

Unmanaged Heap

The unmanaged heap is a region of memory that lives outside the garbage-collected managed heap — typically backed by external SDRAM on devices that have it. Most SITCore SoMs include 32 MB of external SDRAM for this purpose.

Use it when you need:

  • Large buffers (audio samples, sensor capture, file caches) that don't fit comfortably in internal RAM.
  • Graphics framebuffers — the graphics engine automatically allocates from unmanaged heap when external SDRAM is available.

The trade-off vs the managed heap:

  • ✅ Much more space than internal RAM allows
  • ✅ No GC pressure or compaction cost
  • ⚠️ Not secure — external SDRAM can be probed (see External Memory)
  • ⚠️ Manual lifetime — you have to Dispose() buffers yourself

NuGet package: GHIElectronics.TinyCLR.Native.

UnmanagedBuffer​

UnmanagedBuffer is the convenient way to allocate a chunk of unmanaged heap, expose it as a normal byte[], and release it when done. The example below allocates a 100 KB buffer on an SCM20260D Dev Board:

using GHIElectronics.TinyCLR.Native;

// Allocate 100,000 bytes in unmanaged heap.
var uBuffer = new UnmanagedBuffer(100000);
var uTable = uBuffer.Bytes; // Usable as a regular byte[].

// ... use uTable ...

// Release the unmanaged memory — mandatory, the GC won't do it for you.
uBuffer.Dispose();
warning

You must call Dispose() on UnmanagedBuffer — there's no automatic cleanup. Wrap the buffer in a using block if its lifetime fits a single scope.

tip

For sensitive data, encrypt the contents before putting them in the unmanaged buffer. See Cryptography for the available algorithms.

Graphics memory​

When external SDRAM is present, the graphics engine automatically allocates its display buffers, bitmaps, and intermediate render targets from the unmanaged heap — no special API needed. Graphics buffers are exempt from the manual-disposal rule: garbage collection handles graphics buffers in unmanaged heap automatically.

This means a typical graphics-heavy application doesn't see the unmanaged heap directly. It only matters when you're explicitly allocating large buffers via UnmanagedBuffer.

Checking unmanaged heap usage​

Same pattern as managed memory — useful for debugging leaks and tuning buffer sizes:

using GHIElectronics.TinyCLR.Native;
using System.Diagnostics;

Debug.WriteLine("Unmanaged free: " + Memory.UnmanagedMemory.FreeBytes);
Debug.WriteLine("Unmanaged used: " + Memory.UnmanagedMemory.UsedBytes);

API reference​

NamespaceDescription
GHIElectronics.TinyCLR.NativeUnmanaged heap allocation and pointer access