# How to free allocated slice partially?

**URL:** <https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930>\
**Category:** Help\
**Tags:** language, memory-management, standard-library\
**Created:** [October 12, 2026, 9:17pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930 "2026-10-12T21:17:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hizani](https://ziggit.dev/user_avatar/ziggit.dev/hizani/32/5818_2.png) [@hizani](https://ziggit.dev/u/hizani)\
**Post date:** [October 12, 2026, 9:17pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/1 "2026-10-12T21:17:48Z")

</div>

```zig
test "example" {
    const buf = try std.heap.page_allocator.alloc(u8, 10);
    std.heap.page_allocator.free(buf[5..]);
}

```

This test panics with `incorrect alignment` error. I also tried `rawFree` with different alignments, but was getting the same error anyway.

Is it even possible to do this?

In the real code I allocate a huge slice, write some data there, store the filled part and cache the unused one for future usage if it exists. Partial deallocation raises the panic

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [October 12, 2026, 9:24pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/2 "2026-10-12T21:24:03Z")

</div>

Try checking `.shrinkToLen` of an ArrayList. Tldr, there is an `.remap` function on `Allocator`

---

<div class="post-metadata">

**Author:** ![hizani](https://ziggit.dev/user_avatar/ziggit.dev/hizani/32/5818_2.png) [@hizani](https://ziggit.dev/u/hizani)\
**Post date:** [October 12, 2026, 9:38pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/3 "2026-10-12T21:38:11Z")

</div>

remap requires pointer to the begging of the original allocation, but I need to deallocate the second part in the different context where the original slice is no longer available

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [October 12, 2026, 9:51pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/4 "2026-10-12T21:51:04Z")

</div>

This definitely won’t work. It looks like what you want is a custom memory pool (slot map).

---

<div class="post-metadata">

**Author:** ![IceGuye](https://ziggit.dev/letter_avatar_proxy/v4/letter/i/b782af/32.png) [@IceGuye](https://ziggit.dev/u/IceGuye)\
**Post date:** [October 12, 2026, 11:45pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/5 "2026-10-12T23:45:40Z")

</div>

Allocating, reallocating, and freeing strategies are a very complicated topic in computer. Different allocators have their own strategies. But the free() as a function will not do partially like that.

You may need to rethink about your memory allocating and freeing strategies, and you may also need memory pools or try other allocators.

Under the hood, even the ArrayList handles growing data by allocating a whole fixed size memory (2x of size than previous allocating IIRC), then copying the data to them. Removing will not free the memory. It’s shrinking that free the memory by allocating a smaller size and copying the data to it, then freeing the big one…

Frequently allocating and freeing a few slots may need a lot of syscalls which slows the computer down… People keep finding the balance between memory efficiency and speed.

This book is very good to understand how memory allocating work under the hood: [3&nbsp; Memory and Allocators – Introduction to Zig](https://pedropark99.github.io/zig-book/Chapters/01-memory.html)

Andrew’s speech also discussed about the speed of syscalls comparing to many other operations: [https://m.youtube.com/watch?v=IroPQ150F6c&t=387s&pp=2AGDA5ACAQ%3D%3D](https://m.youtube.com/watch?v=IroPQ150F6c&t=387s&pp=2AGDA5ACAQ%3D%3D)

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [October 13, 2026, 12:44am UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/6 "2026-10-13T00:44:12Z")

</div>

Regardless you need to give it the start of the actual allocation, that is the semantics of the api, as it may be necessary depending on the implementation.

To shrink an allocation you would use the `resize` or `remap` functions.

---

<div class="post-metadata">

**Author:** ![truly-not-taken](https://ziggit.dev/user_avatar/ziggit.dev/truly-not-taken/32/6205_2.png) [@truly-not-taken](https://ziggit.dev/u/truly-not-taken)\
**Post date:** [October 13, 2026, 3:23am UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/7 "2026-10-13T03:23:38Z")

</div>

```zig
test "resize" {
    // should work with both std.heap.page_allocator and some other allocators
    // const allocator = std.heap.page_allocator;
    const allocator = std.testing.allocator;
    const len = 1000000;
    // allocate a million bytes
    var buf = try allocator.alloc(u8, len);
    defer allocator.free(buf);
    const new_len = len / 2;
    // use first half million
    @memset(buf[0..new_len], '1');
    // free the remaining half million
    if (allocator.resize(buf, new_len)) {
        buf = buf[0..new_len];
    } else return error.ResizeFailed;
    // do something with buf that is now half million bytes
    try std.testing.expectEqual('1', buf[499999]);
}

```

---

<div class="post-metadata">

**Author:** ![hizani](https://ziggit.dev/user_avatar/ziggit.dev/hizani/32/5818_2.png) [@hizani](https://ziggit.dev/u/hizani)\
**Post date:** [October 13, 2026, 7:37pm UTC](https://ziggit.dev/t/how-to-free-allocated-slice-partially/17930/8 "2026-10-13T19:37:52Z")

</div>

Thanks for the answers!

It seems that in this imperfect world my big brain caching strategy is impossible due to hardware alignment constraints
