Skip to content

Instantly share code, notes, and snippets.

@TylerPachal
Created September 27, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerPachal/434de6c5d467453d073d63f091833de6 to your computer and use it in GitHub Desktop.
Save TylerPachal/434de6c5d467453d073d63f091833de6 to your computer and use it in GitHub Desktop.
# Create a long enough string that it will be stored off of this process' heap
iex(1)> long_binary = "This is a long binary that will need to be at least 64 bytes in length"
"This is a long binary that will need to be at least 64 bytes in length"
# Verify that our string is longer than 64 bytes
iex(2)> byte_size(long_binary)
70
# Inspect the IEx process' binary info. We can see our binary is there by correlating the 70 byte
# length. The tuple that is returned is of the form {binary id, byte length, reference count}. So
# right now we can see that only one thing has a reference to this binary.
iex(3)> :erlang.process_info(self(), :binary)
{:binary, [{515928504, 70, 1}]}
# Create a new ETS table and insert out binary
iex(4)> :ets.new(:my_ets, [:public, :named_table])
:my_ets
iex(5)> :ets.insert(:my_ets, {long_binary, 99})
true
# Instpect the IEx process' binary info again and observe that now the reference count has been
# incremented to 2, since the IEx process and the ETS table are both referencing the binary
iex(6)> :erlang.process_info(self(), :binary)
{:binary, [{515928504, 70, 2}]}
# Delete everything our binary from the ETS table
iex(7)> :ets.delete_all_objects(:my_ets)
true
# Check the IEX process' binary info one last time to confirm that the reference count has gone
# back down to 1
iex(8)> :erlang.process_info(self(), :binary)
{:binary, [{515928504, 70, 1}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment