memory 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2400492846a2a2ac811dbdd279b3c1aa02446e6c2dfd45684e7c517d45ab3139
4
- data.tar.gz: 649b747e821996d0aca2cb92782661916fb05b8c76418a8e95acef988f97909a
3
+ metadata.gz: 116b9be7bb4a7d1c7b906923132406708953a8e3e7d43687d82f7920192ad74f
4
+ data.tar.gz: bf093a25998cefbf01f8fbc79d92d23bc972c016596acb3678cd6303eb09e034
5
5
  SHA512:
6
- metadata.gz: 34682359d1d6b6e04af1582fb9a3a43c798ca032a02fd79fe9f1f62ee00d4c717639867e41f53e998bfc2749afe9423e2a87865451b58de3e63cf033e40c2710
7
- data.tar.gz: e5f1bbcf26cb1aae51c03d2fd465c38682ee35f8d9098af225791802ba5f9c34238d8b86993e10628b047da71c50336826ddf0f2beb1a0a204ffb244887894e0
6
+ metadata.gz: 403676e22f95cd9239f3da72898a9be935c100e236bd51a6fca58d2ebf472a33609efb373dff628cf5f6f1e7ed9ace28dc3abc45adbbdc9975d489539b2f853d
7
+ data.tar.gz: be14f62bb150ab3daec0c9743800e887e606b0ba1545725143484362278779451f9a98dab2a35126d4396d6a57e08c1ccdce28b1a21fe5e894516b16e235b79c
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2020-2023, by Samuel Williams.
4
+ # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
6
  module Memory
7
7
  UNITS = {
@@ -31,7 +31,7 @@ module Memory
31
31
  end
32
32
 
33
33
  def << allocation
34
- self.memory += allocation.size
34
+ self.memory += allocation.memsize
35
35
  self.count += 1
36
36
  end
37
37
 
data/lib/memory/report.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2020-2023, by Samuel Williams.
4
+ # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
6
  require_relative 'aggregate'
7
7
 
8
8
  module Memory
9
9
  class Report
10
- def self.general
10
+ def self.general(**options)
11
11
  Report.new([
12
12
  Aggregate.new("By Gem", &:gem),
13
13
  Aggregate.new("By File", &:file),
@@ -15,10 +15,12 @@ module Memory
15
15
  Aggregate.new("By Class", &:class_name),
16
16
  ValueAggregate.new("Strings By Gem", &:gem),
17
17
  ValueAggregate.new("Strings By Location", &:location),
18
- ])
18
+ ], **options)
19
19
  end
20
20
 
21
- def initialize(aggregates)
21
+ def initialize(aggregates, retained_only: true)
22
+ @retained_only = retained_only
23
+
22
24
  @total_allocated = Aggregate::Total.new
23
25
  @total_retained = Aggregate::Total.new
24
26
 
@@ -38,16 +40,25 @@ module Memory
38
40
  def concat(allocations)
39
41
  allocations.each do |allocation|
40
42
  @total_allocated << allocation
41
- @total_retained << allocation if allocation.retained
42
43
 
43
- @aggregates.each do |aggregate|
44
- aggregate << allocation
44
+ if allocation.retained
45
+ @total_retained << allocation
46
+ end
47
+
48
+ if !@retained_only || allocation.retained
49
+ @aggregates.each do |aggregate|
50
+ aggregate << allocation
51
+ end
45
52
  end
46
53
  end
47
54
  end
48
55
 
49
56
  def print(io = $stderr)
50
- io.puts "\# Memory Profile", nil
57
+ if @retained_only
58
+ io.puts "\# Retained Memory Profile", nil
59
+ else
60
+ io.puts "\# Memory Profile", nil
61
+ end
51
62
 
52
63
  io.puts "- Total Allocated: #{@total_allocated}"
53
64
  io.puts "- Total Retained: #{@total_retained}"
@@ -11,12 +11,14 @@
11
11
  # Copyright, 2018, by Jonas Peschla.
12
12
  # Copyright, 2018, by Espartaco Palma.
13
13
  # Copyright, 2020, by Jean Boussier.
14
- # Copyright, 2020-2022, by Samuel Williams.
14
+ # Copyright, 2020-2024, by Samuel Williams.
15
15
 
16
16
  require 'objspace'
17
17
  require 'msgpack'
18
18
  require 'console'
19
19
 
20
+ require_relative 'cache'
21
+
20
22
  module Memory
21
23
  class Wrapper < MessagePack::Factory
22
24
  def initialize(cache)
@@ -67,6 +69,10 @@ module Memory
67
69
  @allocated = Array.new
68
70
  end
69
71
 
72
+ def inspect
73
+ "#<#{self.class} #{@allocated.size} allocations>"
74
+ end
75
+
70
76
  attr :filter
71
77
 
72
78
  attr :cache
@@ -90,6 +96,9 @@ module Memory
90
96
 
91
97
  # **WARNING** Do not allocate any new Objects between the call to GC.start and the completion of the retained lookups. It is likely that a new Object would reuse an object_id from a GC'd object.
92
98
 
99
+ # Overwrite any immediate values on the C stack to avoid retaining them.
100
+ ObjectSpace.dump(Object.new)
101
+
93
102
  GC.enable
94
103
  3.times{GC.start}
95
104
 
@@ -127,8 +136,8 @@ module Memory
127
136
  @allocated.concat(allocations)
128
137
  end
129
138
 
130
- def report
131
- report = Report.general
139
+ def report(**options)
140
+ report = Report.general(**options)
132
141
 
133
142
  report.concat(@allocated)
134
143
 
@@ -154,7 +163,7 @@ module Memory
154
163
  def track_allocations(generation)
155
164
  rvalue_size = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE]
156
165
 
157
- allocated = Hash.new.compare_by_identity
166
+ allocated = Hash.new
158
167
 
159
168
  ObjectSpace.each_object do |object|
160
169
  next unless ObjectSpace.allocation_generation(object) == generation
@@ -4,8 +4,8 @@
4
4
  # Copyright, 2013-2019, by Sam Saffron.
5
5
  # Copyright, 2015-2016, by Dave Gynn.
6
6
  # Copyright, 2018, by Jonas Peschla.
7
- # Copyright, 2020-2022, by Samuel Williams.
7
+ # Copyright, 2020-2024, by Samuel Williams.
8
8
 
9
9
  module Memory
10
- VERSION = "0.4.1"
10
+ VERSION = "0.5.0"
11
11
  end
data/license.md CHANGED
@@ -24,7 +24,7 @@ Copyright, 2019-2020, by Jean Boussier.
24
24
  Copyright, 2019, by Ashwin Maroli.
25
25
  Copyright, 2019, by Olle Jonsson.
26
26
  Copyright, 2019, by Danny Ben Shitrit.
27
- Copyright, 2020-2023, by Samuel Williams.
27
+ Copyright, 2020-2024, by Samuel Williams.
28
28
 
29
29
  Permission is hereby granted, free of charge, to any person obtaining a copy
30
30
  of this software and associated documentation files (the "Software"), to deal
data/readme.md CHANGED
@@ -49,41 +49,40 @@ report.print
49
49
  ``` ruby
50
50
  memory_sampler = nil
51
51
  config.before(:all) do |example_group|
52
- name = example_group.class.description.gsub(/[^\w]+/, "-")
52
+ name = example_group.class.description.gsub(/[^\w]+/, '-')
53
53
  path = "#{name}.mprof"
54
-
54
+
55
55
  skip if File.exist?(path)
56
-
56
+
57
57
  memory_sampler = Memory::Sampler.new
58
58
  memory_sampler.start
59
59
  end
60
60
 
61
61
  config.after(:all) do |example_group|
62
- name = example_group.class.description.gsub(/[^\w]+/, "-")
62
+ name = example_group.class.description.gsub(/[^\w]+/, '-')
63
63
  path = "#{name}.mprof"
64
-
64
+
65
65
  if memory_sampler
66
66
  memory_sampler.stop
67
-
67
+
68
68
  File.open(path, "w", encoding: Encoding::BINARY) do |io|
69
69
  memory_sampler.dump(io)
70
70
  end
71
-
71
+
72
72
  memory_sampler = nil
73
73
  end
74
74
  end
75
75
 
76
76
  config.after(:suite) do
77
77
  memory_sampler = Memory::Sampler.new
78
-
79
- Dir.glob("*.mprof") do |path|
80
- memory_sampler.load(File.read(
81
- path,
82
- encoding: Encoding::BINARY,
83
- ))
78
+
79
+ Dir.glob('*.mprof') do |path|
80
+ $stderr.puts "Loading #{path}..."
81
+ memory_sampler.load(File.read(path, encoding: Encoding::BINARY))
84
82
  end
85
-
86
- memory_sampler.results.print
83
+
84
+ $stderr.puts "Memory usage:"
85
+ memory_sampler.report.print
87
86
  end
88
87
  ```
89
88
 
@@ -128,4 +127,4 @@ This project uses the [Developer Certificate of Origin](https://developercertifi
128
127
 
129
128
  ### Contributor Covenant
130
129
 
131
- This project is governed by [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
130
+ This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
@@ -61,7 +61,7 @@ cert_chain:
61
61
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
62
62
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
63
63
  -----END CERTIFICATE-----
64
- date: 2023-08-16 00:00:00.000000000 Z
64
+ date: 2024-06-27 00:00:00.000000000 Z
65
65
  dependencies:
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: bake
@@ -125,7 +125,9 @@ files:
125
125
  homepage: https://github.com/socketry/memory
126
126
  licenses:
127
127
  - MIT
128
- metadata: {}
128
+ metadata:
129
+ documentation_uri: https://socketry.github.io/memory/
130
+ source_code_uri: https://github.com/socketry/memory.git
129
131
  post_install_message:
130
132
  rdoc_options: []
131
133
  require_paths:
@@ -134,14 +136,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
136
  requirements:
135
137
  - - ">="
136
138
  - !ruby/object:Gem::Version
137
- version: '3.0'
139
+ version: '3.1'
138
140
  required_rubygems_version: !ruby/object:Gem::Requirement
139
141
  requirements:
140
142
  - - ">="
141
143
  - !ruby/object:Gem::Version
142
144
  version: '0'
143
145
  requirements: []
144
- rubygems_version: 3.4.10
146
+ rubygems_version: 3.5.11
145
147
  signing_key:
146
148
  specification_version: 4
147
149
  summary: Memory profiling routines for Ruby 2.3+
metadata.gz.sig CHANGED
Binary file