You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A scoped (promptly memory-freeing) withMat + withHMatAsMat functions.
Using them ensures that memory is freed as soon as we're done with the
matrices.
This is important because if large matrices are freed only with some
delay, you can get memory resource exhaustion if you call to C.
That is because while memory pressure in Haskell triggers GC and thus
ForeignPtr finalisers, memory pressure in C does not trigger GC in Haskell.
For example, code like this can take a machine out of memory
(assume we have 30 GB RAM in the example):
mat <- hMatToMat -- allocates 20 GB
doSomethingWithMat mat -- ^ above 20 GB are now unused but will be
-- freed only at next GC
bigUnrelatedComputationInC -- tries to allocate another 20 GB => OOM
In comparison, this would not take the same machine out of memory:
withHMatAsMat $ \mat -> do -- allocates 20 GB
doSomethingWithMat mat --
-- ^ above 20 GB are freed promptly here
bigUnrelatedComputationInC -- tries to allocate another 20 GB => no OOM
Large memory buffers should be treated as resources that need to be freed
promptly (like file descriptors) if there are other programming languages
involved that do not not tell Haskell's GC when a GC is really necessary.
0 commit comments