Hi,
In my application, I have the case of a command running in a loop, whose number of repetitions can be relatively large. Within this loop, I create several instances of objects, whose size is also significantly large.
At runtime, I notice that my 32GB of memory is saturated, and the execution time increases significantly with each pass through the loop (because of GC I guess).
What can I do to avoid this, set variables referencing these objects to nil after use?
It would help if you gave us more details. What kind of classes are you creating that need this much memory? What are you actually doing? Code examples?
Generally Crystal is designed in a way that shouldn’t require you to manage memory. The only times you might want to is when C binding.
In terms of how to shave down the memory profile:
- Try determining if all these objects need to be in memory all the time or if they can be serialized and stored somewhere else, or if generating them is less costly than storing them. To do this you need to profile using something like valgrind.
- Determine the actual needed lifecycle of the objects, do they need to exist only for one second, until a task is done, or until the program closes.
- Try running
GC.collect
after large memory operations to suggest the GC run at a particular point.