class Sample
Tracks memory growth for a specific class.
Records allocation counts over time and detects sustained growth patterns that indicate potential memory leaks.
Definitions
def initialize(target, size = 0, threshold: 1000)
Create a new sample profiler for a class.
Signature
-
parameter
targetClass The class being sampled.
-
parameter
sizeInteger Initial object count.
-
parameter
thresholdInteger Minimum increase to consider significant.
Implementation
def initialize(target, size = 0, threshold: 1000)
@target = target
@current_size = size
@maximum_observed_size = size
@threshold = threshold
@sample_count = 0
@increases = 0
end
def sample!(size)
Record a new sample measurement.
Signature
-
parameter
sizeInteger Current object count for this class.
-
returns
Boolean True if count increased significantly.
Implementation
def sample!(size)
@sample_count += 1
@current_size = size
# @maximum_observed_count ratchets up in units of at least @threshold counts.
# When it does, we bump @increases to track a potential memory leak.
if @maximum_observed_size
delta = @current_size - @maximum_observed_size
if delta > @threshold
@maximum_observed_size = size
@increases += 1
return true
end
else
@maximum_observed_size = size
end
return false
end
def as_json(...)
Convert sample data to JSON-compatible hash.
Signature
-
returns
Hash Sample data as a hash.
Implementation
def as_json(...)
{
target: @target.name || "(anonymous class)",
current_size: @current_size,
maximum_observed_size: @maximum_observed_size,
increases: @increases,
sample_count: @sample_count,
threshold: @threshold,
}
end
def to_json(...)
Convert sample data to JSON string.
Signature
-
returns
String Sample data as JSON.
Implementation
def to_json(...)
as_json.to_json(...)
end