# How to manually free allocated memory (Pointer.malloc)

**URL:** <https://forum.crystal-lang.org/t/how-to-manually-free-allocated-memory-pointer-malloc/5691>\
**Category:** Help & Support\
**Created:** [May 22, 2023, 7:47am UTC](https://forum.crystal-lang.org/t/how-to-manually-free-allocated-memory-pointer-malloc/5691 "2023-05-22T07:47:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![romoxi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/romoxi/32/1236_2.png) [@romoxi](https://forum.crystal-lang.org/u/romoxi)\
**Post date:** [May 22, 2023, 7:47am UTC](https://forum.crystal-lang.org/t/how-to-manually-free-allocated-memory-pointer-malloc/5691/1 "2023-05-22T07:47:32Z")

</div>

Hi there,

I use a pointer to allocate some memory:

> @arr = Pointer.malloc(@arr\_index\_max, 0\_f64)

Is it possible to free the memory manually?  
Or how can I tell the GC to free the memory?

Thanx

---

<div class="post-metadata">

**Author:** ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)\
**Post date:** [May 22, 2023, 8:19am UTC](https://forum.crystal-lang.org/t/how-to-manually-free-allocated-memory-pointer-malloc/5691/2 "2023-05-22T08:19:09Z")

</div>

You can un-reference (pointer going out of the scope, pointer assigned to some other value/reference etc) the pointer so that GC can re-claim the memory.

Another possibility here could be you invoke `Pointer#realloc` with lower size or 0.

```crystal
# un-reference
@arr = Pointer(Floa64).null   

# OR

# re-allocate pointer size
@arr = @arr.realloc(0)

```

---

<div class="post-metadata">

**Author:** ![romoxi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/romoxi/32/1236_2.png) [@romoxi](https://forum.crystal-lang.org/u/romoxi)\
**Post date:** [May 28, 2023, 7:40am UTC](https://forum.crystal-lang.org/t/how-to-manually-free-allocated-memory-pointer-malloc/5691/3 "2023-05-28T07:40:35Z")

</div>

I use this little script for testing both versions:

```auto
puts "Start"
ptr = Pointer.malloc(1147483648_u64, 0_f64)
puts "wait"
sleep 3
puts "\nFree the allocated memory."
# ptr = Pointer(Float64).null
ptr = ptr.realloc(0)
puts "is free"
sleep 10
puts "end"

```

According to the task manager, the memory is only released after the script has ended.

---

<div class="post-metadata">

**Author:** ![falsifian](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@falsifian](https://forum.crystal-lang.org/u/falsifian)\
**Post date:** [March 22, 2026, 2:16pm UTC](https://forum.crystal-lang.org/t/how-to-manually-free-allocated-memory-pointer-malloc/5691/4 "2026-03-22T14:16:20Z")

</div>

You’ll find the same thing with a C program. For example:

```auto
#include <stdio.h>                                                               
#include <stdlib.h>                                                              
#include <unistd.h>                                                              
                                                                                 
int main(void)                                                                   
{                                                                                
    char *c = malloc(1147483648);                                                
    for (int i = 0; i < 1147483648; ++i) c[i] = 1;                               
    printf("wait\n");                                                            
    sleep(3);                                                                    
    free(c);                                                                     
    printf("is free\n");                                                         
    sleep(10);                                                                   
    return 0;                                                                    
}             

```

Often, calling `free` doesn’t actually release the memory back to the OS. But if you were to make another allocation in the same execution of the same program, the freed memory would get re-used. So `free` is just telling the memory allocation machinery that the memory can be used again another time `malloc` is called.
