由于地图功能导致的内存泄漏 [英] Thunk memory leak as a result of map function

查看:113
本文介绍了由于地图功能导致的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个多线程程序,它非常适用于mvars;在这种情况下,我有一个线程周期性地改变mvar中的列表。不幸的是,有一个thunk内存泄漏。似乎有一个问题,'地图id'(在真正的程序中,我使用的是别的东西而不是id)函数泄漏。我无法找到如何避免这种情况的方法 - 我在玩'seq'而没有结果。什么是正确的方法来解决泄漏?

  upgraderThread :: MVar [ChannelInfo]  - > IO()
upgraderThread chanMVar = forever job
其中
job = do
threadDelay 1000
vlist< - takeMVar chanMVar
let reslist = map id vlist
putMVar chanMVar reslist


解决方案

尝试,这似乎工作:

pre $ upgraderThread chanMVar =永远工作
其中
工作=做
threadDelay 1000
vlist< - takeMVar chanMVar
let!reslist = strictList $ map id vlist
putMVar chanMVar reslist
$ b $ strictList xs =如果所有p xs那么xs else []
其中px = x`seq` True


I am writing a multi-threaded program that makes quite a use of mvars; in this case I have a thread that periodically changes list inside an mvar. Unfortunately, there is a thunk memory leak. There seems to be aproblem that the 'map id' (in real program I use something else than id) function leaks. I just cannot find a way how to avoid that - I was playing with 'seq' with no result. What is the right way to fix the leak?

upgraderThread :: MVar [ChannelInfo] -> IO ()
upgraderThread chanMVar = forever job
    where
        job = do
            threadDelay 1000
            vlist <- takeMVar chanMVar
            let reslist = map id vlist
            putMVar chanMVar reslist

解决方案

After a few more tries, this one seems to work:

upgraderThread chanMVar = forever job
    where
        job = do
            threadDelay 1000
            vlist <- takeMVar chanMVar
            let !reslist = strictList $ map id vlist
            putMVar chanMVar reslist

        strictList xs = if all p xs then xs else []
            where p x = x `seq` True        

这篇关于由于地图功能导致的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆