How Erlang atoms can be garbage collected

前端 未结 3 872
一整个雨季
一整个雨季 2020-12-20 21:17

It is said that atoms are not garbage collected. Once you’ve created an atom, it remains in the atom table, which might cause memory leakage at the end of the day!

I

3条回答
  •  执笔经年
    2020-12-20 21:32

    While I'm not sure atoms are garbage-collected, you can easily do without worrying whether you will blow up the system's memory. As @Chiron said, as long as all your atoms are known at compile time you should be ok.

    What if I really need to use list_to_atom/1 somehow? Well, you may be able to twist your issue using this kind of function:

    atom("apple") -> apple;
    atom("orange") -> orange;
    atom("banana") -> banana.
    

    One other workaround is list_to_existing_atom/1

    But the VM can still eat more and more RAM: other connected Erlang nodes may register atoms globally, that is allocate atoms at run time.

提交回复
热议问题