Skip to content

Instantly share code, notes, and snippets.

@redink
Created September 20, 2016 08:36
Show Gist options
  • Save redink/177dca332e58b2bf7c59e694b57aac8f to your computer and use it in GitHub Desktop.
Save redink/177dca332e58b2bf7c59e694b57aac8f to your computer and use it in GitHub Desktop.
-module(test_route_binary_leak).
-behaviour(gen_server).
-export([start_link/0]).
-export([test_route_binary_leak/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-define(FAKEPID, list_to_pid("<0.11111.0>")).
-record(state, {}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
test_route_binary_leak(Binary) ->
gen_server:call(?SERVER, {test_route_binary_leak, Binary}).
init([]) ->
{ok, #state{}}.
handle_call({test_route_binary_leak, ThisBigBinary}, _From, State) ->
erlang:send(?FAKEPID, {send_binary_to_fake_pid, ThisBigBinary}),
% {reply, ok, State, hibernate};
{reply, ok, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
@redink
Copy link
Author

redink commented Sep 20, 2016

test 1

use hibernate when callback return.

$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> {ok, Pid} = test_route_binary_leak:start_link().
{ok,<0.35.0>}
2> erlang:memory(binary).
128552
3> [test_route_binary_leak:test_route_binary_leak(binary:copy(erlang:integer_to_binary(X), X)) || X <- lists:seq(1, 100000)].
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,ok,
 ok,ok,ok,ok,ok,ok,ok,ok,ok,ok|...]
4> erlang:memory(binary).
134512
5> 

memory for binary did not change obviously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment