原文 2015-01-12 00:11:27 发表于 CSDN,这里对以前写的文章做下收录。
erlang 提供 binary_to_term,把一个二进制数据转为原始的 erlang 数据。但是,这个函数也有副作用。如果直接将对端传来的数据做一次 binary_to_term,就有可能导致VM crash掉。
binary_to_term 副作用
这是因为二进制数据带有原子时,binary_to_term 会生成这些原子。但VM原子总数是有限制,而且原子不参与GC。假如数据带有原子数量超过erlang VM限制,就会导致VM crash
所以,针对这个问题,erlang提供了另外一个函数 binary_to_term(Binary, [safe]),这个函数不会生成新的的原子,在这点上,这两个函数关系也像 list_to_atom 和 list_to_existing_atom
1> term_to_binary(atom).
<<131,100,0,4,97,116,111,109>>
2> binary_to_term(v(1), [safe]).
atom
3> binary_to_term(v(1)).
atom
4> binary_to_term(<<131,100,0,4,116,101,115,116>>, [safe]).
%%这里会抛出异常
** exception error: bad argument
in function binary_to_term/2
called as binary_to_term(<<131,100,0,4,116,101,115,116>>,[safe])
5> binary_to_term(<<131,100,0,4,116,101,115,116>>).
test
6> binary_to_term(<<131,100,0,4,116,101,115,116>>, [safe]).
%%这里就不会抛出异常
test
