haskell-fswatch / hfsnotify

Unified Haskell interface for basic file system notifications
BSD 3-Clause "New" or "Revised" License
136 stars 40 forks source link

Memory leak on OS X #35

Open sethfowler opened 11 years ago

sethfowler commented 11 years ago

There seems to be a memory leak when using this library on OS X. I am not sure if it comes from code in System.FSNotify or from the underlying System.OSX.FSEvents, though.

gregwebs commented 11 years ago

Thanks for the report. Can you give a minimal test program that creates the memory leak?

sethfowler commented 11 years ago

Sure. This program is enough to do it:

{-# LANGUAGE OverloadedStrings #-}

import System.FSNotify
import System.IO

main = withManager doWatch

doWatch m = do
  putStrLn "doWatch is starting. Do something that will cause large amounts of"
  putStrLn "disk activity in the current directory; my test case is to compile"
  putStrLn "Firefox. Press Enter when things settle down to terminate. This"
  putStrLn "program should easily reach hundreds of megabytes of memory"
  putStrLn "when compiling a large program like Firefox."
  watchTree m "." (const True) (\e -> putStrLn $ "Got event " ++ (show e))
  getLine >> return ()

Instructions are included. =) If you want to try with my exact test case, you can grab a copy of the Firefox source using hg clone http://hg.mozilla.org/mozilla-central and build it with make -f client.mk.

I just ran this twice and checked the final memory usage as reported by Activity Monitor. I got 442.9 MB on the first run and 650.7 MB on the second.

gregwebs commented 11 years ago

One thing that might be related to this: I had slow performance with const True on a large code base, but once I filtered down to the files I actually needed it worked fine.

sethfowler commented 11 years ago

It's very possible that they're related, yeah. In my real application I filter out everything but C++ source files and the leak does seem slower. If filtered out events don't contribute to the leak, that is quite interesting. I'm not 100% sure there's actually an effect, though, because there's so much variance in the leak size.

sethfowler commented 11 years ago

The hfsevents package comes with a trace program in the tests directory that does something very similar to what my test program above does. The difference is that trace does not leak memory. I'm afraid that the leak does not appear to be in System.OSX.FSEvents. It must be in this package.

sethfowler commented 10 years ago

@gregwebs I'm thinking of trying to get this fixed sometime in the near future. Which repo and branch should I work from?

gregwebs commented 10 years ago

the rc branch. Thanks!

thomasjm commented 8 years ago

@sethfowler I realize this was 2 years ago, but did you ever make any progress on memory leak issues? I'm running into it on Ubuntu linux as well. Setting up a watch with a broad predicate (essentially just const True) causes big memory usage.

In addition, I noticed that if I trigger a large number of files changes by invoking chown -R on the watched folder, then the memory usage becomes even bigger (like hundreds of megabytes in my case)

sethfowler commented 8 years ago

@thomasjm I didn't, unfortunately. I just ended up periodically killing and restarting the thread on a timer.

thomasjm commented 6 years ago

Note to self: try replacing Chans with strict-concurrency versions and see if it helps

simonmichael commented 4 years ago

We see this with hledger-ui too. [Using watchDir, with a restrictive predicate matching only a few files.]