原创文章,转载请注明出处:服务器非业余研究http://blog.csdn.net/erlib 作者Sunface
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