Erlang Process StructureEach Erlang process includes a process control block (PCB) , a stack and a private heap.A PCB is a data structrue containing process management information,such as process ID(IDentifier) postion of stack and heap,argument registers and program counter.Besides the heap,there might be some small heap fragments which are merged into themain heap after each memory garbage collection . The heap fragments are used when there is no enough free space in the heapand garbage collection cannot be performed there is not enough free space in the heap and garbage collection cannot be performed to get more free memory.For instance , when a process is sending a message to another process ,if the receiving process doesn't have enough heap space to accommodate the incoming message,the sending process doesn't invoke a garbage collection for the receiving process in the SMP VM.In addition ,binaries larger than 64 bytes are stored in a common heap shared by all
processes.ETS tables are also stored in a common heap.Figure 3.1 illustrates there main memory areas (there are also other memory areas,such as for atom table).As Figure 3.1 shows , the stack and heap of an Erlang process are located in the same continuous memory area which is allocated and managed together.From the standpoint of an OS process or
thread,this area belongs to its heap,which means the stack and heap of an Erlang process actually are stored in the heap of its VM.In the area,the heap starts at the lowest address and grows upwards ,while the stack starts at the highest address and grows
downwards.Heap overflow can be detected by examining the heap top and the stack top. 每个Erlang 进程都有一个进程控制块PCB(Process control block),一个stack和一个heap。PCB 是一个用来存储管理进程信息的数据结构。例如进程唯一id,栈和堆的指针,注册的参数和程序的计数等等。除了heap,里面也许还有一些小型的堆碎片,这些碎片会在内存回收后合并到heap里面去。堆碎片只有在堆里面没有足够的空间并且gc没法提供更多的内存的时候被使用。举个例子,当进程发送消息给其他进程的时候,如果接受消息的进程没有足够的堆空间来接受将要到来的消息,那么发送消息的进程不会触发接受进程的垃圾回收。此外,二进制数据如果大于64B,将会被存储到一个公共的二进制存储空间,这块空间将会被所有的进程共享。ETS
表也同样会被存储到一个公共的堆。上图3.1列出了这些主要内存区域。还有一些其他的内存区域没有被列举出来,比如原子的空间。如图3.1所示,栈和堆在一块连续的内存区域,被共同管理和分配。从操作系统进程或者线程的角度来看,这块内存区域是属于VM自己的。这也就意味着Erlang进程的堆和栈实际上都被存储在VM的堆上。在这块区域上,堆开始于低地址向上增长。然后栈起始于高地址,向下增长。堆数据溢出可以通过堆的top和栈的top来判断。堆通常用来存储一些混合的数据结构,比如元组列表或者大数据等等。但是栈却用来存储简单的数据结构和堆中存储的复杂数据结构的引用或者指针。为了方便gc,这里面没有从堆到栈的引用或者指针。