TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Understanding Memory Fragmentation in Haskell

67 pointsby tirumaraiselvanover 4 years ago

5 comments

FooBarWidgetover 4 years ago
A year ago, I research similar memory issues in Ruby. The established hypothesis in the community was that it was due to memory fragmentation. But I found that the largest culprit was actually the glibc memory allocator, which doesn&#x27;t like to return memory to the OS. In multithreaded scenarios this issue is amplified even more, due to the use of separate heap arenas per thread.<p>I also found a simple solution: call malloc_trim() after a GC. This reduces memory usage by 70%.<p><a href="https:&#x2F;&#x2F;www.joyfulbikeshedding.com&#x2F;blog&#x2F;2019-03-14-what-causes-ruby-memory-bloat.html" rel="nofollow">https:&#x2F;&#x2F;www.joyfulbikeshedding.com&#x2F;blog&#x2F;2019-03-14-what-caus...</a>
评论 #24243053 未加载
评论 #24244271 未加载
评论 #24243159 未加载
评论 #24242770 未加载
sirabenover 4 years ago
Having written Haskell for over a year now for personal projects, understanding the memory model can be one of the hardest aspects of Haskell, which can make it frustrating to write allocation-free code (although some techniques like deforestation are done by the compiler to eliminate intermediate structures entirely).<p>Linear types being added in GHC 8.12 would be a big deal because it would allow programmers to be able to write allocation-free code that can use mutable data structures with a pure API (as opposed to the ST state monad), much like how Rust solves this with the ownership system.
评论 #24242272 未加载
tirumaraiselvanover 4 years ago
More discussion here: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;haskell&#x2F;comments&#x2F;id8m9w&#x2F;welltyped_understanding_memory_fragmentation&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;haskell&#x2F;comments&#x2F;id8m9w&#x2F;welltyped_u...</a>
brundolfover 4 years ago
Is this sort of multi-tier allocation used in any other garbage collected languages? Or is it specifically used in Haskell because of immutability (which would presumably to result in a higher-than-normal frequency of allocation&#x2F;deallocation)?
评论 #24243364 未加载
评论 #24244564 未加载
评论 #24242882 未加载
croteover 4 years ago
Wouldn&#x27;t the GC be able to `munmap` the space between blocks?<p>Sure, it wouldn&#x27;t solve object-level fragmentation, but at least you&#x27;d get rid of block-level fragmentation.
评论 #24243425 未加载