Hash reported as retained
Closed this issue · 4 comments
MemoryProfiler will report that an object of type Hash
is being retained by the following code:
require "memory_profiler"
def run
{{:a => 1} => 2}
end
MemoryProfiler.report { run }.pretty_print
Output:
Total allocated: 696 bytes (3 objects)
Total retained: 232 bytes (1 objects)
allocated memory by gem
-----------------------------------
696 other
allocated memory by file
-----------------------------------
696 report.rb
allocated memory by location
-----------------------------------
464 report.rb:5
232 report.rb:4
allocated memory by class
-----------------------------------
696 Hash
allocated objects by gem
-----------------------------------
3 other
allocated objects by file
-----------------------------------
3 report.rb
allocated objects by location
-----------------------------------
2 report.rb:5
1 report.rb:4
allocated objects by class
-----------------------------------
3 Hash
retained memory by gem
-----------------------------------
232 other
retained memory by file
-----------------------------------
232 report.rb
retained memory by location
-----------------------------------
232 report.rb:5
retained memory by class
-----------------------------------
232 Hash
retained objects by gem
-----------------------------------
1 other
retained objects by file
-----------------------------------
1 report.rb
retained objects by location
-----------------------------------
1 report.rb:5
retained objects by class
-----------------------------------
1 Hash
I've actually observed similar behaviour when using an array or a custom class as hash key.
What does it mean the Hash is being retained in such cases? What do you think is preventing the object being unallocated?
@SamSaffron any chance you can have a quick look into this? 😊
It is legitimately being retained see:
https://gist.github.com/e58a0c9743d1927e65e6968f7c050e52
Recommend you open an issue with Ruby core here
@SamSaffron brilliant, thanks for confirming I was not (yet) going crazy with tracking down a memory leak. Will let you know once I've forwarded this to ruby core devs.
@SamSaffron FYI I've been suggested by ruby devs that this is not a case of memory leak: the hash is most probably not being immediately deallocated due to the GC being conservative. https://bugs.ruby-lang.org/issues/16278
It's unfortunate that object space analysis doesn't help in such cases.