You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/memory/cache.rb
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -63,12 +63,12 @@ def lookup_class_name(klass)
63
63
# @parameter obj [String] The string object to cache.
64
64
# @returns [String] A cached copy of the string (truncated to 64 characters).
65
65
deflookup_string(obj)
66
-
# This string is shortened to 64 characters which is what the string report shows. The string report can still list unique strings longer than 64 characters separately because the object_id of the shortened string will be different.
67
-
@string_cache[obj] ||= String.new << obj[0,64]
66
+
# This string is shortened to 64 characters which is what the string report shows. The string report (by value) can still list unique strings longer than 64 characters separately because the `object_id` of the shortened string will be different.
67
+
@string_cache[obj] ||= obj[0,64]
68
68
rescueRuntimeError=>error
69
69
# It is possible for the String to be temporarily locked from another Fiber which raises an error when we try to use it as a hash key. i.e: `Socket#read` locks a buffer string while reading data into it. In this case we `#dup`` the string to get an unlocked copy.
Copy file name to clipboardExpand all lines: test/memory/sampler.rb
+42Lines changed: 42 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -59,6 +59,48 @@ class MyThing
59
59
memory.report# buffer string is locked while reading ObjectSpace#each_object
60
60
end
61
61
62
+
it"does not retain string references"do
63
+
# Ruby 3.2 has different shared string behaviour:
64
+
skip_unless_minimum_ruby_version("3.3")
65
+
66
+
# Strings longer than 23 characters share a reference to a "shared" frozen string which should also be GC'd
67
+
sampler.rundo
68
+
5.timesdo |i|
69
+
short_text="SHORT TEXT ##{i}"
70
+
short_text.dup
71
+
72
+
long_text="LONG TEXT ##{i} 12345678901234567890123456789012345678901234567890"
73
+
long_text.dup
74
+
75
+
very_long_text="VERY LONG TEXT ##{i} 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890"
76
+
very_long_text.dup
77
+
78
+
# Prevent the last frozen string from being the return value of the block:
79
+
nil
80
+
end
81
+
end
82
+
83
+
# 30 strings should be allocated (5 iterations * 6 strings per iteration):
0 commit comments