def test a_on_the_stack : Int32 = 2 puts a_on_the_stack end
a_on_the_stack of type
Int32 is allocated on the stack, so there is no GC involved. When the stack is popped, the variable goes away.
If instead I wanted it to go to the heap, I would do:
def test a_on_the_heap = Int32.new(2) puts a_on_the_heap end
a_on_the_heap goes out of scope after the method returns, I assume that the GC would eventually kick in to free the memory allocated on the heap by the variable
a_on_the_heap above. Is that correct?
My question then becomes:
Is there any way in Crystal to free the allocated heap memory manually, without having to rely on the GC?
def test a_on_the_heap = Int32.new(2) puts a_on_the_heap free a_on_the_heap # <=========== HERE end