The*_*ost 3 profiling haskell ghc
我这样做:
import qualified Data.ByteString.Lazy.Char8 as BS
main :: IO ()
main = do
let filename = "sample.txt"
text <- BS.readFile filename
let res = BS.take 1000 $ text
print res
Run Code Online (Sandbox Code Playgroud)
当我通过分析运行它时,它给了我:
162,048 bytes allocated in the heap
2,472 bytes copied during GC
59,688 bytes maximum residency (1 sample(s))
22,232 bytes maximum slop
156 MB total memory in use (0 MB lost due to fragmentation)
Run Code Online (Sandbox Code Playgroud)
我读的文件大约是50K字节.为什么需要60K字节的内存(最大驻留时间)?我也尝试过String和Lazy文本.这是相同的图片.我认为Haskell在某种程度上是将整个文件读入内存或只是分配与文件长一样多的内存.我怎么能阻止这个?我想从它只读N字节,不想浪费这么多的内存.