A method asking for data from ets is copied, or referenced?

A method asking for data from ets is copied, or referenced?
Since data is immutable, I feel It should be referenced only until it mutates??

Idea is that data is already occupying space in ets/ram, copying in our method is just unnecessary overhead.
Useful when traversing the whole data of ets.
And doing operations on it one by one

Data retrieved from :ets is copied to each process that asks for it.

The reason has to do with Erlang’s independent garbage collection. If processes shared memory, then garbage collection would have to pause many processes in order to track which values were used or not.

By copying values into each process’s RAM, it uses more memory, but it allows each process to have its memory garbage collected individually.

6 Likes

The best answer I ever got.

what about functions that not part of process but still calls to ets.
And the limit of 64 bytes , after which message get referenced.

When code is executing on the BEAM, a process is running.

2 Likes

Yeah as @al2o3cr notes, every function is called from within a process.

For binaries specifically, larger binaries are ref counted between processes. This works because a binary is a contiguous region of memory, so you can basically just ref count the pointer to the start of the binary and you’re good to go. For data structures like lists, arrays, tuples, etc where the contents of those structures can be other arbitrarily complex terms the overhead of ref counting each sub part would be worse than just copying it into each process.

2 Likes

Exactly,. but which one , same or different

I don’t understand the question.