How to turn off the GC in Crystal and do memory management yourself, without any GC?
I'm evaluating the Crystal programming language for real-time, low-latency applications, that can't be interrupted by the garbage collector.
Two questions:
Is it possible to deallocate some class from the heap? For example, in the loop below:
while true
p = Person.new
# do stuff with p
free p # <-------- HERE
end
Is there a flag like -verbose:gc
in Java, that allows us to see everything the GC is doing?
Cheers!
Solution 1:
You can free any heap allocated memory with GC.free
. This is unsafe and potentially dangerous, which you surely are aware of.
Crystal uses BDWGC, so you can use its runtime configuration options (such as GC_PRINT_STATS
) to enable logging.
If you want, you can entirely disable the garbage collector by passing the -Dgc_none
flag. But the entire standard library assumes to be used with garbage collection and does not do any manual memory management. So if you use stdlib, it will inevitably pile up heap memory.