Hi, I am trying to know if the keys in defstruct are atoms, and if so what is the maximum amount of defstruct instance I can create dynamically.
For example, I am creating an online game server, it has a Player struct with four keys, I create a new player when a client joins the game. If the keys are atoms, then they should be a limit on how many players can join the game due to the cap on atoms in the Beam.
Please let me know if there is anything I am missing, thanks.
Yes, keys in structs are atoms.
And you can have zero or one defstruct
per module.
Though, as player1
and player2
share the same set of atomkeys, you do not have a problem.
1 Like
They are atoms, but if you have a player struct with 4 keys, and you create 10 players, that’s still only 4 atoms, not 40. If you have some atom :name
, every instance of :name
is exactly the same atom. The BEAM limit on atoms is about dynamically generating entirely new atoms. For example 1..1000 |> Enum.map(fn i -> String.to_atom("name#{i}") end)
would generate 1000 new atoms :name1, :name2, etc
.
3 Likes