Term sharing in Erlang/OTP 下篇


 
转载自http://www.cnblogs.com/me-sa/p/term_sharing_in_erlang_otp_two.html
L0 = [0],

L1 = [L0, L0, L0, L0, L0, L0, L0, L0, L0, L0],
 
L2 = [L1, L1, L1, L1, L1, L1, L1, L1, L1, L1],
 
L3 = [L2, L2, L2, L2, L2, L2, L2, L2, L2, L2],
 
L4 = [L3, L3, L3, L3, L3, L3, L3, L3, L3, L3],
 
L5 = [L4, L4, L4, L4, L4, L4, L4, L4, L4, L4],
 
L6 = [L5, L5, L5, L5, L5, L5, L5, L5, L5, L5],
 
L7 = [L6, L6, L6, L6, L6, L6, L6, L6, L6, L6],
 
L8 = [L7, L7, L7, L7, L7, L7, L7, L7, L7, L7],
 
L9 = [L8, L8, L8, L8, L8, L8, L8, L8, L8, L8],
 
L = [L9, L9, L9, L9, L9, L9, L9, L9, L9, L9],
 
L.

  

影响有多大呢?看结果:
 
After a bit more of 45 minutes of struggling, the compiler tries to allocate 3.7 GB of memory and gives up:

$ erlc demo.erl
Crash dump was written to: erl_crash.dump
eheap_alloc: Cannot allocate 3716993744 bytes of
memory (of type "heap_frag").
Abort

 

  好吧,勇于自黑,由于上面遇到这样让人恼火的问题,我决定在Shell中完成后续的测试,然后,我一脚踏进"新坑":

 

陷阱2 Shell ! Shell !

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Eshell V6.0  (abort with ^G)
1> L=[1,2,3,4,5,6,7,8,9,10].
[1,2,3,4,5,6,7,8,9,10]
2>  L2=[L,L,L,L,L,L].
[[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]]
3> erts_debug:size(L2).
32
4> erts_debug:flat_size(L2).
132
5> io:format( "~p" ,[L2]).
[[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]]ok
6> erts_debug:size(L2).
32
7> erts_debug:flat_size(L2).
132

  

 

    一开始启动shell的时候,Shell的Pid是<0.33.0>.然后我们在中间故意执行一个不存在的方法 fake:fake().这时查看一下,Shell已经重启,Pid变成<0.40.0>.注意再执行erts_debug:size(L2).结果已经变成了132了,换句话说,这里L2数据已经展开了.

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Eshell V6.0  (abort with ^G)
1> self().
<0.33.0>
2>  L=[1,2,3,4,5,6,7,8,9,10].
[1,2,3,4,5,6,7,8,9,10]
3>  L2=[L,L,L,L,L,L].
[[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]]
4>  erts_debug:size(L2).
32
5> erts_debug:flat_size(L2).
132
6> fake:fake().
** exception error: undefined function fake:fake/0
7> self().
<0.40.0>
8>  erts_debug:size(L2).
132
9> erts_debug:flat_size(L2).
132
10>

  

 

   那为什么会触发数据展开(expand ,flattening)呢? 看下面的代码,在Shell启动的时候,会把之前已经绑定的变量作为spawn_link参数以启动新的shell.

 

 

?
1
2
3
4
5
6
7
erl6.2\lib\stdlib-2.2\src
 
start_eval(Bs, RT, Ds) ->
     Self = self(),
     Eval = spawn_link(fun() -> evaluator(Self, Bs, RT, Ds) end),
     put(evaluator, Eval),
     Eval.

  

  换句话说,Erlang中使用spawn创建进程,传入的参数(包括函数闭包),需要拷贝到新进程的heap,换句话说进程创建的时候需要考虑参数的大小.

 

  OK,这个问题差不多了,休息.

你可能感兴趣的:(erlang)