|
54 | 54 |
|
55 | 55 | -include("rabbit_memory.hrl").
|
56 | 56 |
|
| 57 | +-define(MEM_CACHE_EXP, 500). |
| 58 | + |
57 | 59 | %%----------------------------------------------------------------------------
|
58 | 60 |
|
59 | 61 | -type vm_memory_high_watermark() :: (float() | {'absolute', integer() | string()}).
|
@@ -111,17 +113,29 @@ get_memory_limit() ->
|
111 | 113 |
|
112 | 114 | get_memory_use(bytes) ->
|
113 | 115 | MemoryLimit = get_memory_limit(),
|
114 |
| - {get_process_memory(), case MemoryLimit > 0.0 of |
| 116 | + {get_process_memory_cached(), case MemoryLimit > 0.0 of |
115 | 117 | true -> MemoryLimit;
|
116 | 118 | false -> infinity
|
117 | 119 | end};
|
118 | 120 | get_memory_use(ratio) ->
|
119 | 121 | MemoryLimit = get_memory_limit(),
|
120 | 122 | case MemoryLimit > 0.0 of
|
121 |
| - true -> get_process_memory() / MemoryLimit; |
| 123 | + true -> |
| 124 | + Cached = get_process_memory_cached(), |
| 125 | + Cached / MemoryLimit; |
122 | 126 | false -> infinity
|
123 | 127 | end.
|
124 | 128 |
|
| 129 | +get_process_memory_cached() -> |
| 130 | + Now = time_compat:erlang_system_time(milli_seconds), |
| 131 | + case ets:lookup(?MODULE, memory) of |
| 132 | + [{memory, Val, ExpTime}] when ExpTime >= Now -> Val; |
| 133 | + _ -> |
| 134 | + Mem = get_process_memory(), |
| 135 | + ets:insert(?MODULE, {memory, Mem, Now + ?MEM_CACHE_EXP}), |
| 136 | + Mem |
| 137 | + end. |
| 138 | + |
125 | 139 | %% Memory reported by erlang:memory(total) is not supposed to
|
126 | 140 | %% be equal to the total size of all pages mapped to the emulator,
|
127 | 141 | %% according to http://erlang.org/doc/man/erlang.html#memory-0
|
@@ -225,6 +239,7 @@ start_link(MemFraction, AlarmSet, AlarmClear) ->
|
225 | 239 | [MemFraction, {AlarmSet, AlarmClear}], []).
|
226 | 240 |
|
227 | 241 | init([MemFraction, AlarmFuns]) ->
|
| 242 | + ets:new(?MODULE, [named_table, public]), |
228 | 243 | TRef = erlang:send_after(?DEFAULT_MEMORY_CHECK_INTERVAL, self(), update),
|
229 | 244 | State = #state { timeout = ?DEFAULT_MEMORY_CHECK_INTERVAL,
|
230 | 245 | timer = TRef,
|
|
0 commit comments