I have 8 GB of RAM, but Haskell programs seemingly can only use 1.3 GB.
I’m using this simple program to determine how much memory a GHC program can allocate:
import System.Environment
import Data.Set as Set
main = do
args <- getArgs
let n = (read $ args !! 0) :: Int
s = Set.fromList [0..n]
do
putStrLn $ "min: " ++ (show $ findMin s)
putStrLn $ "max: " ++ (show $ findMax s)
Here’s what I’m finding:
- running
./mem.exe 40000000 +RTS -ssucceeds and reports1113 MB total memory in use - running
./mem.exe 42000000 +RTS -sfails without of memory error - running
./mem.exe 42000000 +RTS -s -M4Gerrors out with-M4G: size outside allowed range - running
./mem.exe 42000000 +RTS -s -M3.9Gfails without of memory error
Monitoring the process via the Windows Task Manager shows that the max memory usage is about 1.2 GB.
My system: Win7, 8 GB RAM, Haskell Platform 2011.04.0.0, ghc 7.0.4.
I’m compiling with: ghc -O2 mem.hs -rtsopts
How can I make use of all of my available RAM? Am I missing something obvious?
Currently, on Windows, GHC is a 32-bit GHC – I think a 64-bit GHC for windows is supposed to be available when 7.6 comes.
One consequence of that is that on Windows, you can’t use more than
4G - 1BLOCKof memory, since the maximum allowed as a size-parameter isHS_WORD_MAX:With 32-bit Words,
HS_WORD_MAX = 2^32-1.That explains
since
decodeSize()decodes4Gas2^32.This limitation will remain also after upgrading your GHC, until finally a 64-bit GHC for Windows is released.
As a 32-bit process, the user-mode virtual address space is limited to 2 or 4 GB (depending on the status of the
IMAGE_FILE_LARGE_ADDRESS_AWAREflag), cf Memory limits for Windows Releases.Now, you are trying to construct a
Setcontaining 42 million 4-byteInts. AData.Set.Sethas five words of overhead per element (constructor, size, left and right subtree pointer, pointer to element), so theSetwill take up about 0.94 GiB of memory (1.008 ‘metric’ GB). But the process uses about twice that or more (it needs space for the garbage collection, at least the size of the live heap).Running the programme on my 64-bit linux, with input 21000000 (to make up for the twice as large
Ints and pointers), I getbut
topreports only1.1gof memory use –top, and presumably the Task Manager, reports only live heap.So it seems
IMAGE_FILE_LARGE_ADDRESS_AWAREis not set, your process is limited to an address space of 2GB, and the 42 millionSetneeds more than that – unless you specify a maximum or suggested heap size that is smaller:Setting the maximal heap size below what it would use naturally, actually lets it fit in hardly more than the space needed for the
Set, at the price of a slightly longer GC time, and suggesting a heap size of-H1800Mlets it finish using onlySo if you specify a maximal heap size below 2GB (but large enough for the
Setto fit), it should work.