Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 3f11e2d

Browse files
author
Daniil Fedotov
committed
Do not use interval timer to update the process state.
If the process takes too long to update in the interval time, the interval will fill it's message box. System RSS memory reporting can be overloaded by constant requests if the message box contains many 'update' messages.
1 parent 9f28ac4 commit 3f11e2d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/vm_memory_monitor.erl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ start_link(MemFraction, AlarmSet, AlarmClear) ->
225225
[MemFraction, {AlarmSet, AlarmClear}], []).
226226

227227
init([MemFraction, AlarmFuns]) ->
228-
TRef = start_timer(?DEFAULT_MEMORY_CHECK_INTERVAL),
228+
TRef = erlang:send_after(?DEFAULT_MEMORY_CHECK_INTERVAL, self(), update),
229229
State = #state { timeout = ?DEFAULT_MEMORY_CHECK_INTERVAL,
230230
timer = TRef,
231231
alarmed = false,
@@ -243,8 +243,14 @@ handle_call(get_check_interval, _From, State) ->
243243
{reply, State#state.timeout, State};
244244

245245
handle_call({set_check_interval, Timeout}, _From, State) ->
246-
{ok, cancel} = timer:cancel(State#state.timer),
247-
{reply, ok, State#state{timeout = Timeout, timer = start_timer(Timeout)}};
246+
State1 = case erlang:cancel_timer(State#state.timer) of
247+
false ->
248+
State#state{timeout = Timeout};
249+
_ ->
250+
State#state{timeout = Timeout,
251+
timer = erlang:send_after(Timeout, self(), update)}
252+
end,
253+
{reply, ok, State1};
248254

249255
handle_call(get_memory_limit, _From, State) ->
250256
{reply, State#state.memory_limit, State};
@@ -256,7 +262,10 @@ handle_cast(_Request, State) ->
256262
{noreply, State}.
257263

258264
handle_info(update, State) ->
259-
{noreply, internal_update(State)};
265+
erlang:cancel_timer(State#state.timer),
266+
State1 = internal_update(State),
267+
TRef = erlang:send_after(State1#state.timeout, self(), update),
268+
{noreply, State1#state{ timer = TRef }};
260269

261270
handle_info(_Info, State) ->
262271
{noreply, State}.
@@ -381,10 +390,6 @@ emit_update_info(AlarmState, MemUsed, MemLimit) ->
381390
"vm_memory_high_watermark ~p. Memory used:~p allowed:~p~n",
382391
[AlarmState, MemUsed, MemLimit]).
383392

384-
start_timer(Timeout) ->
385-
{ok, TRef} = timer:send_interval(Timeout, update),
386-
TRef.
387-
388393
%% According to http://msdn.microsoft.com/en-us/library/aa366778(VS.85).aspx
389394
%% Windows has 2GB and 8TB of address space for 32 and 64 bit accordingly.
390395
get_vm_limit({win32,_OSname}) ->

0 commit comments

Comments
 (0)