# Specify backing allocator to std.heap.DebugAllocator

**URL:** <https://ziggit.dev/t/specify-backing-allocator-to-std-heap-debugallocator/17023>\
**Category:** Help\
**Tags:** language, memory-management, standard-library, testing\
**Created:** [August 1, 2026, 10:06am UTC](https://ziggit.dev/t/specify-backing-allocator-to-std-heap-debugallocator/17023 "2026-08-01T10:06:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapusta](https://ziggit.dev/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kapusta](https://ziggit.dev/u/kapusta)\
**Post date:** [August 1, 2026, 10:06am UTC](https://ziggit.dev/t/specify-backing-allocator-to-std-heap-debugallocator/17023/1 "2026-08-01T10:06:44Z")

</div>

Hello, I wanted to create test where function would run against FixedBufferAllocator to check if it conforms to my memory constrainsts, but at the same time I wanted to track allocations like with DebugAllocator, so I thought that maybe I can limit usable adress space by allocator by constructing it myself, or add backing fixedBufferAllocator, but that seemed to not be the case (at least in 0.16)

So what would be best way to approach running a test with limited memory?

Running test twice, once with Debug and once with FixedBuffer allocators? Using FixedBuffer, but backed by Debug one, writing wrapper around Debug, that would track used address space?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [August 1, 2026, 10:54am UTC](https://ziggit.dev/t/specify-backing-allocator-to-std-heap-debugallocator/17023/2 "2026-08-01T10:54:38Z")

</div>

You can pass a Config struct into the DebugAllocator:

```zig
pub fn DebugAllocator(comptime config: Config) type {

```

One of the options sounds exactly like what you are looking for:

```zig
    /// If true, the allocator will have two fields:
    /// * `total_requested_bytes` which tracks the total allocated bytes of memory requested.
    /// * `requested_memory_limit` which causes allocations to return `error.OutOfMemory`
    /// when the `total_requested_bytes` exceeds this limit.
    /// If false, these fields will be `void`.
    enable_memory_limit: bool = false,

```

Putting it all together:

```zig
var debug_allocator: std.heap.DebugAllocator(.{.enable_memory_limit = true}) = .init;
debug_allocator.requested_memory_limit = 1234;

```

To answer the original question: You can also configure a custom backing allocator (though note that the debug allocator may not work optimally under all circumstances when not backed by the page allocator):

```zig
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
debug_allocator.backing_allocater = fixed_buffer_allocator;

```

For the next time you have a question like this, I’d also suggest looking at the source code (it’s surprisingly easy to read and you can learn a lot from it) or checking out the [generated stdlib documentation](https://ziglang.org/documentation/master/std/#std.heap.DebugAllocator).

---

<div class="post-metadata">

**Author:** ![kapusta](https://ziggit.dev/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kapusta](https://ziggit.dev/u/kapusta)\
**Post date:** [August 1, 2026, 3:21pm UTC](https://ziggit.dev/t/specify-backing-allocator-to-std-heap-debugallocator/17023/3 "2026-08-01T15:21:19Z")

</div>

Oh, I must have missed that enable\_memory\_limit when I looked at Config structure, thanks.
