brendanhay / amazonka

A comprehensive Amazon Web Services SDK for Haskell.
https://amazonka.brendanhay.nz
Other
599 stars 227 forks source link

Memory leak in amazonka #1003

Closed jappeace closed 4 weeks ago

jappeace commented 1 month ago

Running the following code:

main :: IO ()
main = do
  env <- newEnv discover
  forever $ do
    currentTime <- getCurrentTime
    putStrLn ("loop " <> show currentTime)
    void $ runResourceT $
      send env newListBuckets

With "-with-rtsopts=-N -K1K -M83M" will fail after a couple cycles, for example:

loop 2024-08-19 18:52:50.277515098 UTC
loop 2024-08-19 18:52:51.099626077 UTC
loop 2024-08-19 18:52:51.287630764 UTC
loop 2024-08-19 18:52:51.474031168 UTC
loop 2024-08-19 18:52:51.645261749 UTC
loop 2024-08-19 18:52:51.826526469 UTC
loop 2024-08-19 18:52:52.0134773 UTC
loop 2024-08-19 18:52:52.193782581 UTC
loop 2024-08-19 18:52:52.364554697 UTC
loop 2024-08-19 18:52:52.553530947 UTC
loop 2024-08-19 18:52:52.73949618 UTC
loop 2024-08-19 18:52:52.92592668 UTC
loop 2024-08-19 18:52:53.115882587 UTC
loop 2024-08-19 18:52:53.301737753 UTC
loop 2024-08-19 18:52:53.494226766 UTC
loop 2024-08-19 18:52:53.677115509 UTC
loop 2024-08-19 18:52:53.849280692 UTC
loop 2024-08-19 18:52:54.037181755 UTC
loop 2024-08-19 18:52:54.219637351 UTC
loop 2024-08-19 18:52:54.390515217 UTC
loop 2024-08-19 18:52:54.560147996 UTC
loop 2024-08-19 18:52:54.769981021 UTC
loop 2024-08-19 18:52:54.956664218 UTC
loop 2024-08-19 18:52:55.127418872 UTC
loop 2024-08-19 18:52:55.316635496 UTC
loop 2024-08-19 18:52:55.505725741 UTC
loop 2024-08-19 18:52:55.695297234 UTC
loop 2024-08-19 18:52:55.885898178 UTC
loop 2024-08-19 18:52:56.059205365 UTC
loop 2024-08-19 18:52:56.231575115 UTC
loop 2024-08-19 18:52:56.401184049 UTC
loop 2024-08-19 18:52:56.584703691 UTC
loop 2024-08-19 18:52:56.773821208 UTC
loop 2024-08-19 18:52:56.957273263 UTC
loop 2024-08-19 18:52:57.143328987 UTC
loop 2024-08-19 18:52:57.333481896 UTC
loop 2024-08-19 18:52:57.521643351 UTC
loop 2024-08-19 18:52:57.711487929 UTC
loop 2024-08-19 18:52:57.89965251 UTC
loop 2024-08-19 18:52:58.085954488 UTC
loop 2024-08-19 18:52:58.276777813 UTC
loop 2024-08-19 18:52:58.486182791 UTC
loop 2024-08-19 18:52:58.669386698 UTC
loop 2024-08-19 18:52:58.840070719 UTC
loop 2024-08-19 18:52:59.027960752 UTC
loop 2024-08-19 18:52:59.215007796 UTC
exe: Heap exhausted;
exe: Current maximum heap size is 87031808 bytes (83 MB).
exe: Use `+RTS -M<size>' to increase it.

The heap shouldn't build up at all.

See full reproducer repo: https://github.com/jappeace/amazonka-reproduce/blob/master/src/Template.hs

My current work around is to write a custom recursive function and use send, which seems to leak less. I also noted that not using -N drastically reduces the amount of heap memory used by the program (from 85 to around 18), and seems to mitigate this leak

jappeace commented 4 weeks ago

Actually looks fine if I increase the limit to 200, it just needs more then 83M after a while.

image

NB: I did have a memory leak but not due to amazonka. I got better at using the profiler while investigating this. It was caused by addKatipContext being used recursively.

endgame commented 3 weeks ago

It's good that you have runResourceT inside the loop, because http requests can leak until you exit the ResourceT: https://github.com/snoyberg/http-client/issues/537

It might be worth splitting the loop so you can reuse the manager for ~20 requests or so (to take a number from a hat). But that's independent of your addKatipContext space leak.