Stuff Goes Bad:Erlang In Anger


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().

 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.
 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.

 首先,所有的返回值都是字节(bytes)为单位的,他们表示内存被分配(Erlang VM实际使用的内存,不是操作系统给Erlang VM分配的内存)。所以它迟早总会比操作系统报告的内存小得多(随意VM的版本更新,总会把Erlang VM使用的内存优化使用量更少一些)。

 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

 + used 用于分配Erlang数据的内存使用报告;
 + allocated VM自己保留的内存分配报告。它包括了内存的使用,也包括还已由OS分配给VM但没有被使用的内存。如果你和ulimit和OS-reported值打交道,这些值非常有用。
 + unused VM保留但没有被分配的没使用内存报告。
 + usage 返回值可能是一个百分比(0.0..1.0)的内存分配比率值。

[11] See Section 7.2
[12] See Section 7.3.2