我使用了
+RTS -p -RTS
运行我的代码,并从报告中得到了以下有趣的信息:total time = 1.08 secs
total alloc = 3,037,568,120 bytes
COST CENTRE MODULE %time %alloc
readPixelMap Main 33.0 11.0
readPixelMap.getByte Main 32.7 75.1
readPixelMap.getColor Main 27.0 13.3
看起来我的程序在 readPixelMap
函数中分配了 大量 的内存。该函数如下:
readPixelMap width height = do
pixels <- replicateM height (replicateM width getColor)
return $ PixMap pixels
where getByte = liftM toInteger getWord8
getColor = do (red:green:blue:alpha:xs) <- replicateM 4 getByte
return (red, green, blue, alpha)
一个PixMap
被定义为
data PixMap = PixMap [[RGBAColor]] deriving (Show)
type RGBAColor = (Integer, Integer, Integer, Integer)
readPixelMap
是使用来自 Data.Binary
的 Get
单子调用的:
main = do
binary <- BS.readFile "test.tga"
let (pixelMap, binary', nil) = runGetState (readPixelMap 2048 2048) binary 0
我一直以为一个2048 x 2048的.TGA图像应该只需要几百兆内存(最多)。有没有明显的方法来提高我的垃圾收集时间/分配数量?