Skip to content

Instantly share code, notes, and snippets.

@devonestes
Created August 1, 2018 10:17
Show Gist options
  • Save devonestes/abe4dc60484e5426d4890c4494430194 to your computer and use it in GitHub Desktop.
Save devonestes/abe4dc60484e5426d4890c4494430194 to your computer and use it in GitHub Desktop.
Reproduction escript for trace/3 wierdness
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname wtf
main(_) ->
process_flag(trap_exit, true),
ToMap = lists:seq(1, 50),
MapFun = fun() ->
lists:map(fun(N) -> N end, ToMap)
end,
start_runner(MapFun),
timer:sleep(5000).
start_runner(MapFun) ->
spawn_link(fun() ->
Printer = start_printer(),
start_tracer(self(), Printer),
measure_memory(MapFun, Printer)
end).
measure_memory(MapFun, Printer) ->
Printer ! process_info(self(), garbage_collection_info),
MapFun(),
Printer ! process_info(self(), garbage_collection_info).
start_printer() ->
spawn(fun() -> print_loop() end).
print_loop() ->
receive
Elem ->
io:format("~w\n\n", [Elem]),
print_loop()
end.
start_tracer(Runner, Printer) ->
spawn(fun() ->
erlang:trace(Runner, true, [garbage_collection, {tracer, self()}]),
tracer_loop(Printer)
end).
tracer_loop(Printer) ->
receive
Elem ->
Printer ! Elem,
tracer_loop(Printer)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment