Skip to content

Instantly share code, notes, and snippets.

@hwatkins
Last active August 29, 2015 14:05
Show Gist options
  • Save hwatkins/fcd2cd57a612782eea0e to your computer and use it in GitHub Desktop.
Save hwatkins/fcd2cd57a612782eea0e to your computer and use it in GitHub Desktop.
Tools for memory analysis
erlang:memory(total).
%% GC all processes
[erlang:garbage_collect(Pid) || Pid <- processes()].
[{K,V / math:pow(1024,3)} || {K,V} <- erlang:memory()].
[{T, ets:info(T, memory)} || T <- ets:all()].
[{T, mnesia:table_info(T, memory)} || T <- mnesia:system_info(tables)].
[{T, ets:info(T, memory)*erlang:system_info(wordsize)} || T <- ets:all(), ets:info(T, memory)*erlang:system_info(wordsize) >1024*1024]
% T=Table Name
ets:foldl(fun(X, Sum) -> io:format("~p~n",[erts_debug:flat_size(X)*erlang:system_info(wordsize)]), erts_debug:flat_size(X)*erlang:system_info(wordsize) + Sum end, 0, T).
%% http://erlang.org/pipermail/erlang-questions/2012-September/069337.html
%% Good discussion on differnt memory carriers
%% https://groups.google.com/forum/#!topic/erlang-programming/xtC9aBvIDcI
mnesia:info().
erlang:process_info(list_to_pid("<0.160.0>"), memory).
%%%
%%% Copyright Ericsson AB 2012. All Rights Reserved.
%%%
%%% The contents of this file are subject to the Erlang Public License,
%%% Version 1.1, (the "License"); you may not use this file except in
%%% compliance with the License. You should have received a copy of the
%%% Erlang Public License along with this software. If not, it can be
%%% retrieved online at http://www.erlang.org/.
%%%
%%% Software distributed under the License is distributed on an "AS IS"
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%%% the License for the specific language governing rights and limitations
%%% under the License.
%%%
%%%-------------------------------------------------------------------
%%% @author Rickard Green <[email protected]>
%%% @copyright (C) 2012, Ericsson AB
%%% @doc
%%%
%%% @end
%%% Created : 24 Sep 2012 by Rickard Green <[email protected]>
%%%-------------------------------------------------------------------
-module(memory).
-export([carriers_size/0]).
carriers_size() ->
AUAs = try
erlang:system_info(alloc_util_allocators)
catch
error:badarg ->
[temp_alloc, eheap_alloc, binary_alloc, ets_alloc,
sl_alloc, ll_alloc, std_alloc]
end,
case asz(aua_info(AUAs)) of
notsup -> erlang:error(notsup);
Res -> Res
end.
aua_info(AUAs) ->
try
erlang:system_info({allocator_sizes, AUAs})
catch
error:badarg ->
try
[{AUA, erlang:system_info({allocator_sizes, AUA})} || AUA <- AUAs]
catch
error:badarg ->
try
[{AUA, erlang:system_info({allocator, AUA})} || AUA <- AUAs]
catch
error:badarg ->
notsup
end
end
end.
asz(notsup) -> notsup;
asz([]) -> 0;
asz([{sbmbc_alloc, false} | As]) -> asz(As);
asz([{_, false} | _]) -> notsup;
asz([{_, AIs} | As]) -> aicsz(AIs) + asz(As).
aicsz([]) -> 0;
aicsz([{instance, _, Cs}| AIs]) -> ctsz(Cs) + aicsz(AIs);
aicsz(Cs) -> ctsz(Cs).
ctsz([]) -> 0;
ctsz([{Xbcs, CI} | CTs]) when Xbcs == mbcs; Xbcs == sbcs -> cisz(CI) + ctsz(CTs);
ctsz([_ | CTs]) -> ctsz(CTs).
cisz([]) -> 0;
cisz([{carriers_size, Sz, _, _} | Cs]) -> Sz + cisz(Cs);
cisz([_ | Cs]) -> cisz(Cs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment