Project

General

Profile

Bug #11101 ยป mem.rb

tkalmus (Thomas Kalmus), 04/27/2015 03:13 PM

 
1
#!/usr/local/ruby-2.1.3/bin/ruby
2

    
3

    
4
#### methods ####
5

    
6
def time_step( n)
7
    while Time.new - $start_time < n
8
        sleep( 0.1)
9
    end
10
end
11

    
12

    
13
def memory_object( size)
14
    result = []
15
    count  = size/20
16
    count.times do
17
        result << "%20.18f" % rand
18
    end
19
    return result
20
end
21

    
22

    
23
def memory_log( message)
24
    shared_memory  = 0
25
    private_memory = 0
26
    lines = File.read( "/proc/#{$$}/smaps")
27
    lines.scan( /.+?Shared_Dirty:\s+(\d+).+?Private_Dirty:\s+(\d+)/m) do |shared_dirty,private_dirty|
28
        shared_memory  += shared_dirty.to_i
29
        private_memory += private_dirty.to_i
30
    end
31
    time = Time.new - $start_time
32
    puts "%6.3fs %5d %-15s %10d %10d" % [time, $$, message, shared_memory/1000, private_memory/1000]
33
end
34

    
35

    
36
##### main #####
37

    
38

    
39
puts "ruby version #{RUBY_VERSION}"
40

    
41
GC.disable
42

    
43
puts " %6s %5s %-15s %10s %10s" % ["time", "pid", "message", "shared",  "private"]
44

    
45
big_memory = memory_object( 10_000_000)
46

    
47
$start_time = Time.new
48

    
49
pid = fork do
50
    time_step( 4)
51
    memory_log( "Child  pre GC")
52

    
53
    time_step( 6)
54
    GC.enable; GC.start; GC.disable
55

    
56
    time_step( 8)
57
    memory_log( "Child  post GC")
58

    
59
    STDOUT.flush
60
    exit!
61
end
62

    
63
time_step( 4)
64
memory_log( "Parent pre GC ")
65

    
66
time_step( 8)
67
memory_log( "Parent post GC")
68

    
69
Process.wait( pid)