手动gc拯救内存。。

抄了别人的代码随便改了改:
process_gc(MemMax) ->          
	filelib:ensure_dir("./log/"),
	File = "./log/processes_infosM.log",
	{ok, Fd} = file:open(File, [write, raw, binary, append]),
	Fun = fun(PID) ->
				  case erlang:process_info(PID,memory) of
					  {memory, Memory}->
						  case Memory > MemMax of
							  true ->
								  Pi = erlang:process_info(PID),
								  io:format("mem:~w  with pid:~w ~n~n",[Memory,PID]),
								  Info = io_lib:format("~p=>~p \n\n",[PID,Pi]),
								  erlang:garbage_collect(PID),
								  case  filelib:is_file(File) of
									  true   ->   file:write(Fd, Info);
									  false  ->
										  file:close(Fd),
										  {ok, NewFd} = file:open(File, [write, raw, binary, append]),
										  file:write(NewFd, Info)
								  end;
							  false ->
								  ignore
						  end;
					  _ ->
						  ignore
				  end,
				  timer:sleep(20)
		  end,
	[   Fun(P) ||   P <- erlang:processes()].   

跑一跑内存就回来几十个G。。


erlang:memory().

erlang:process_info().

参数列表:

	ItemList = [backtrace,binary,catchlevel,current_function,current_location,current_stacktrace
 			   ,dictionary,garbage_collection,group_leader,heap_size,initial_call,links,last_calls
 			   ,memory,messages,min_heap_size,min_bin_vheap_size,monitored_by
 			   ,monitors,reductions,registered_name,stack_size,status,suspending,total_heap_size],
估计要研究下系统参数了。。

http://www.cnblogs.com/me-sa/archive/2011/11/13/2246951.html

==================

查看占用内存最高的进程:

spawn(fun() -> etop:start([{output, text}, {interval, 1}, {lines, 20}, {sort, memory}]) end). (以输出text方式启动etop,其间隔为1秒,输出行数为20行,按照内存排序. 这里spawn一个新进程,目的是输出etop数据时不影响erlang shell 输入.) etop输出有点乱,超过一定范围变成了**,不过我们已经找到了内存占用最高的进程.

发布了75 篇原创文章 · 获赞 2 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章