[Erlang危機](5.1.1)記憶體,erlang5.1.1
原創文章,轉載請註明出處:伺服器非業餘研究http://blog.csdn.net/erlib 作者Sunface聯絡郵箱:cto@188.com
Memory
The memory reported by the Erlang VM in most tools will be a variant of what is reported by erlang:memory() :
Erlang VM大多數檢測記憶體的工具都是通過erlang:memory()來實現的。
-------------------------------------------------------------------------------------
1> erlang:memory().
[{total,13772400},
{processes,4390232},
{processes_used,4390112},
{system,9382168},
{atom,194289},
{atom_used,173419},
{binary,979264},
{code,4026603},
{ets,305920}]
--------------------------------------------------------------------------------------
This requires some explaining.
First of all, all the values returned are in bytes, and they represent memory allocated (memory actively used by the Erlang VM, not the memory set aside by the operating system for the Erlang VM). It will sooner or later look much smaller than what the operating system reports.
這裡需要解釋下:
首先,所有的傳回值都是位元組(bytes)為單位的,它們表示目前被分配的記憶體(Erlang VM實際使用的記憶體,不是作業系統給Erlang VM分配的記憶體),所以這個值要比作業系統分配的小很多。
The total field contains the sum of the memory used for processes and system (which is incomplete, unless the VM is instrumented!). processes is the memory used by Erlang processes, their stacks and heaps. system is the rest: memory used by ETS tables, atoms in the VM, refc binaries11, and some of the hidden data I mentioned was missing.
If you want the total amount of memory owned by the virtual machine, as in the amount that will trip system limits (ulimit), this value is more difficult to get from within the VM.
If you want the data without calling top or htop, you have to dig down into the VM’s memory allocators to find things out12.
總欄位包含了所有進程和系統(除instrumented模式外)的總記憶體佔用大小。返回的processes項是指Erlang進程使用的堆棧總記憶體。system項就包含其餘的:ETS表,VM中的原子,位元據11(refc binaries,具體的可見堅強2002部落格 - Sunface),和一些我沒有提及到的隱藏資料。
如果你想得到VM在作業系統佔用的總記憶體,這個值在訪問系統的限制下(ulimit),很難從VM內部獲得。
如果你想不調用top或htop命令來得到資料,你就不得不深入VM記憶體管理分配來找到你想要的12。
Fortunately, recon has the function recon_alloc:memory/1 to figure it out, where the argument is:
• used reports the memory that is actively used for allocated Erlang data;
• allocated reports the memory that is reserved by the VM. It includes the memory used, but also the memory yet-to-be-used but still given by the OS. This is the amount you want if you’re dealing with ulimit and OS-reported values.
• unused reports the amount of memory reserved by the VM that is not being allocated. Equivalent to allocated-used.
• usage returns a percentage (0.0 .. 1.0) of used over allocated memory ratios. There are additional options available, but you’ll likely only need them when investigating memory leaks in chapter 7
不過,很幸運的是,recon有一個函數:recon_alloc:memory/1可以解決上述問題,參數如下:
used:給Erlang data分配的記憶體
allocated:VM佔用的總記憶體。它包括已使用的記憶體,也包括還已由OS分配給VM但尚未被分配的記憶體。如果你在處理ulimit和
OS-reported的值,這個參數就非常有用。
unused:OS分配給VM但是尚未被VM分配的那部分記憶體。
usage:返回各個功能使用記憶體的百分比。還有一些額外的選項,不過你可能只會在第七章的記憶體流失那裡使用。
[11] See Section 7.2
[12] See Section 7.3.2
[注11]:參見章節7.2
[注12]:參見章節7.3.2