Maximum heap size (-M) should take megablock usage into account
Motivation
The -M
RTS flag lets us place a limit on the size of the heap. As we approach the limit, garbage collections will happen more often and the RTS will try to keep the amount of live data below this amount. If during a garbage collection, it finds the live data to exceed this amount then the program is terminated.
Yet, as far as I can tell, the current logic doesn't take fragmentation into account. It is possible to have a situation where the live data is below the limit but the size of the heap exceeds the limit, as fragmentation means that the RTS cannot free blocks/megablocks.
Proposal
At the end of GC, we already try to free megablocks to put us below the limit. But if we fail then we should kill the program with a heap overflow. Right now that doesn't happen.
We should also make sure that if megablock usage is higher than the memory limit, that we trigger a major collection.
I can implement this if this sounds OK.