Skip to content

Commit

Permalink
Try different cleanup strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexJF committed Dec 20, 2023
1 parent 9dba020 commit 83c429b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions spec/datadog/profiling/stack_recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def sample_allocation(obj)
._native_sample(Thread.current, stack_recorder, metric_values, labels, numeric_labels, 400, false)
end

before do
def do_starting_allocations
allocations = [a_string, an_array, "a fearsome interpolated string: #{sample_rate}", (-10..-1).to_a, a_hash,
{ 'z' => -1, 'y' => '-2', 'x' => false }]
@num_allocations = 0
Expand All @@ -374,8 +374,25 @@ def sample_allocation(obj)
@num_allocations += 1
end

allocations.clear # The literals in the previous array are now dangling
GC.start # And this will clear them, leaving only the non-literals which are still pointed to by the lets
# Do a thorough cleanup of the allocations array. We witnessed some flakiness
# with just a simple clear call which is why we added the map and put this all
# in the same function.
allocations.map! { nil }
allocations.clear

# At this point plus the return from this call, there should be no way for any
# object to still be considered alive in any way (for example, in some CPU register)
nil
end

before do
# Do a bunch of allocations, cleaning up references to all, except the objects kept
# alive by the lets. We could inline this but the extra call should hopefully help
# clear any unintentional references or remains in CPU registers and such.
do_starting_allocations
# This GC should collect those sampled allocations that are not being kept alive by
# the lets.
GC.start
end

context 'when disabled' do
Expand Down

0 comments on commit 83c429b

Please sign in to comment.