。内存泄漏
内存泄漏原因主要是:
- 在进程字典中存储较大数据结构
- behavior state data 无限的增长(存储一个增长的list/proplist)
- 在底层跨进程共享较大的二进制数据
解决办法:
-避免动态创建atom(使用list_to_existing_atom/1或binary_to_existing_atom/1)
-避免使用进程字典(如果确要使用的话,要确保清除不再使用的键值)
-避免增长behaviour state(使用像Riak一样的持久方案)
-避免使用较大的二进制数据
。如何调试
Actually, GCs happen very frequently in Erlang(you can manually GC by calling erlang:garbage_collect(Pid)), as soon as a process
heap fills up. The heap size is increased if removing the garbage is
not sufficient to allocate the next term on the heap.
Among the things suggested so far, I'd also look at the process
dictionary. It will be part of the output from sys:get_status/1 or
process_info/1, but e.g.
erts_debug:flat_size(process_info(Pid, dictionary))
could be a way to monitor it.
Another useful technique is to use
erlang:system_monitor(MonitorPid, [{large_heap, Limit}])
and having the MonitorPid call process_info/1 on the process in
question, any time it receives a large_heap message.