Memory Management in Ruby

I am puzzled by the ruby's behavior and how it controls memory.

I understand the behavior of Ruby GC (primary or secondary) if the number of objects is counted above a threshold or limit (i.e. heap_available_slots, old_objects_limit, remembered_shady_object_limit, malloc_limit). Ruby starts / starts GC (primary or minor).

And after GC, if it cannot find enough memory, Ruby allocates (basically malloc, I assume) more memory for the current program.

In addition, this is a well-known fact that immediately frees up the RAM of the OS.

Now..

What I don’t understand is why Ruby releases memory (back to the OS) without running any GC.

Example

require 'rbtrace'

index = 1
array = []
while(index < 20000000) do 
   array << index
   index += 1
end


sleep 10
print "-"
array=nil
sleep

. ruby ​​2.2.2p95. htop RSS (test.rb PID 11483), 161 .

enter image description here

GC.stat ( rbtrace gem) ( GC count)

rbtrace -p 11843 -e '[Time.now,Process.pid,GC.stat]'

[Time.now,Process.pid,GC.stat]
=> [2016-07-27 13:50:28 +0530, 11843, 
{
  "count": 7,
  "heap_allocated_pages": 74,
  "heap_sorted_length": 75,
  "heap_allocatable_pages": 0,
  "heap_available_slots": 30162,
  "heap_live_slots": 11479,
  "heap_free_slots": 18594,
  "heap_final_slots": 89,
  "heap_marked_slots": 120,
  "heap_swept_slots": 18847,
  "heap_eden_pages": 74,
  "heap_tomb_pages": 0,
  "total_allocated_pages": 74,
  "total_freed_pages": 0,
  "total_allocated_objects": 66182,
  "total_freed_objects": 54614,
  "malloc_increase_bytes": 8368,
  "malloc_increase_bytes_limit": 33554432,
  "minor_gc_count": 4,
  "major_gc_count": 3,
  "remembered_wb_unprotected_objects": 0,
  "remembered_wb_unprotected_objects_limit": 278,
  "old_objects": 14,
  "old_objects_limit": 10766,
  "oldmalloc_increase_bytes": 198674592,
  "oldmalloc_increase_bytes_limit": 20132659
}]
*** detached from process 11843

GC count => 7 

25 . 6 , GC 7.

enter image description here

[Time.now,Process.pid,GC.stat]
=> [2016-07-27 14:16:02 +0530, 11843, 
{
  "count": 7,
  "heap_allocated_pages": 74,
  "heap_sorted_length": 75,
  "heap_allocatable_pages": 0,
  "heap_available_slots": 30162,
  "heap_live_slots": 11581,
  "heap_free_slots": 18581,
  "heap_final_slots": 0,
  "heap_marked_slots": 120,
  "heap_swept_slots": 18936,
  "heap_eden_pages": 74,
  "heap_tomb_pages": 0,
  "total_allocated_pages": 74,
  "total_freed_pages": 0,
  "total_allocated_objects": 66284,
  "total_freed_objects": 54703,
  "malloc_increase_bytes": 3248,
  "malloc_increase_bytes_limit": 33554432,
  "minor_gc_count": 4,
  "major_gc_count": 3,
  "remembered_wb_unprotected_objects": 0,
  "remembered_wb_unprotected_objects_limit": 278,
  "old_objects": 14,
  "old_objects_limit": 10766,
  "oldmalloc_increase_bytes": 198663520,
  "oldmalloc_increase_bytes_limit": 20132659
}]

: , Ruby Release memory , GC. , .

, ( , , , GC.), .

OS: OS X version 10.11.12
+4
1
+4

Source: https://habr.com/ru/post/1649262/


All Articles