Ruby hash memory leak after key removal

Helo, I cannot succeed, how to free memory after deleting the key in the hash. When I delete a key from Hash, the memory is not freed, but after a GC.startmanual call . Is this the expected behavior or does the GC not free up memory when keys are removed from the Hash and these objects leak somewhere? How to remove a key from Hash in Ruby and unallocate it also in memory?

Example:

irb(main):001:0> `ps -o rss= -p #{Process.pid}`.to_i
=> 4748
irb(main):002:0> a = {}
=> {}
irb(main):003:0> 1000000.times{|i| a[i] = "test #{i}"}
=> 1000000
irb(main):004:0> `ps -o rss= -p #{Process.pid}`.to_i
=> 140340
irb(main):005:0> 1000000.times{|i| a.delete(i)}
=> 1000000
irb(main):006:0> `ps -o rss= -p #{Process.pid}`.to_i
=> 140364
irb(main):007:0> GC.start
=> nil
irb(main):008:0> `ps -o rss= -p #{Process.pid}`.to_i
=> 127076

PS: I am using ruby ​​1.8.7. I also tried ruby ​​1.9.2, but that was no better.

+5
source share
4 answers

See fooobar.com/questions/3089 / ...

( ) .

, malloc free C Ruby . , Ruby GC'ed .

+6

, , :

, , C, , , , Ruby, Python Perl.

, Perl, Ruby Python, . , . , , , . ; , . , C .

, , , . , , , Ruby, , - . , , -, , , - , , - , , , , , . , - , , , , . , , , , -, , Apple , , .

, , , , , , . , , , , OO, Ruby. .

, , 1 000 000 - , , . 1 000 000 , . . , , .

+2

. , , . , Ruby , , .

, , , .

+1
source

That @digitalross can be more or less visible if you do the highlighting twice. If there was such a memory leak, you would expect the memory size to double, but this will not happen.

[~]$ irb                                         rvm:ruby-1.9.3-p0@global 
1.9.3p0 :001 > `ps -o rss= -p #{Process.pid}`.to_i
 => 8148 
1.9.3p0 :002 > a = {}
 => {} 
1.9.3p0 :003 > 1000000.times{|i| a[i] = "test #{i}"}
 => 1000000 
1.9.3p0 :004 > `ps -o rss= -p #{Process.pid}`.to_i
 => 101188 
1.9.3p0 :005 > 1000000.times{|i| a.delete(i)}
 => 1000000 
1.9.3p0 :006 > `ps -o rss= -p #{Process.pid}`.to_i
 => 90960 
1.9.3p0 :007 > GC.start
 => nil 
1.9.3p0 :008 > `ps -o rss= -p #{Process.pid}`.to_i
 => 93388 
1.9.3p0 :009 > `ps -o rss= -p #{Process.pid}`.to_i
 => 93388 
1.9.3p0 :010 > 1000000.times{|i| a[i] = "test #{i}"}
 => 1000000 
1.9.3p0 :011 > `ps -o rss= -p #{Process.pid}`.to_i
 => 140088 
1.9.3p0 :012 > 1000000.times{|i| a.delete(i)}
 => 1000000 
1.9.3p0 :013 > `ps -o rss= -p #{Process.pid}`.to_i
 => 130880 
1.9.3p0 :014 > GC.start
 => nil 
1.9.3p0 :015 > `ps -o rss= -p #{Process.pid}`.to_i
 => 104256 

At the end of the first run, the process reports a memory size of 93388, after the second run it reports 104256, only a 10% increase in memory usage.

0
source

All Articles