[erlang-questions] Is there something like fprof but for memory profiling?

Michael Radford <>
Mon Feb 9 23:03:59 CET 2009


Sergey S writes:
> I'm looking for a tool which can help me to find out how much memory a
> function has consumed during its execution.

For a single invocation, you can get a rough answer by spawning a fresh
process:

Measure =
  fun (F) ->
    Parent = self (),
    Pid = spawn (fun () ->
		   { memory, M0 } = erlang:process_info (self (), memory),
		   F (),
		   { memory, M1 } = erlang:process_info (self (), memory),
		   Parent ! { self (), M1 - M0 }
		 end),
    receive
      { Pid, Bytes } when is_integer (Bytes) -> Bytes
    end
  end.

2> Measure (fun () -> ok end). 
2440
3> Measure (fun () -> lists:seq(1,100000) end).
1267296

But you are probably interested in measuring an average over a long run
of your server...

For that, I have no good answer, except maybe look at running the
emulator in instrumented mode (-instr) and see if the output of
instrument:descr(instrument:memory_data()) is helpful.  It will give
you blocks allocated by process id, which can often narrow things down
to a single module, but not a single function.  Unfortunately, the
instrumented emulator adds a lot of overhead, so your program will run
slow and require a lot more memory.

It would be fantastic to have a Erlang tool like valgrind's massif,
which tagged each allocation with a stacktrace and showed you statistics
on heap usage over time.

Mike



More information about the erlang-questions mailing list