Memory Leak on Windows?

100.times do |id|
   spawn do
     i = 0
     loop do
      puts "#{id}: #{i}"
       i += id
	   sleep 0.1


When I run the above code, the taskmanager in windows is showing me a constant increase of the process memory consumption.

This doesn’t happen when the ‘put “#{id}: #{i}”’ line is commented out.

My assumption would be, that the temporary strings will be somehow garbage collected.

Running the same code in linux has a constant process memory consumption.

Crystal 1.10.1 [c6f3552] (2023-10-13)

LLVM: 17.0.2
Default target: x86_64-pc-windows-msvc

Welcome to the Crystal Forum!

That is actually interesting. Usually, the garbage collector should collect all these strings after some time, but for some reason, it doesn’t here (although I’m testing this with Crystal 1.8.0).

You can even manually trigger the collection with GC.collect just before the sleep 0.1 line, after which everything works perfectly fine.

Maybe there’s still some issue with Windows and Fibers, but I don’t think that this behavior is intended. I used some longer strings (without manual GC call) and I stopped the program after it claimed more than 4 GB of my RAM…

After calling GC.collect manually, process memory consumption is going to an upper limit => okay

This should be done automatically, right ?

Yes - and if you don’t use any fibers in your code it actually works fine.

For some reason the fibers seem to mess up the regular garbage collection on Windows.

I’m currently thinking of transfering a Ruby project to Crystal. Unfortunately I have to run the code on windows and I want to use several fibers.

My impression is that not too many people are using crystal on windows.

Is there a change that “somebody” is resolving this issue in the near future ?

This issue seems to be related:

It’s marked as “Closed”, but the discussion is quite lengthy and often mentioned by other issues (even recently), so maybe that issue might need to be reopened (at least for Windows).

As mentioned, as a workaround you could run GC.collect from time to time (like every 5 minutes), but yeah, this issue needs to be fixed (or at least a clarification what’s going on).

Thanks for your detailed answers !

I would rather open a new issue, since this is platform-specific. We’re working now on ironing Windows right now, so this is the right moment to put that issue (can’t promise a fix soon though).

1 Like