手動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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章