[erlang-questions] Binary memory usage problems. Ref holding, how to debug?

Vans S <>
Sun Dec 4 17:37:50 CET 2016

So I have an app that decodes very large JSON using jiffy into maps.  The size of a single json ends up being 8mb or more.

Using 'instrument' module we can see the memory_data().

Afaik 187 TypeNo is binary? Based on the index of
binary in MemoryData = {term(), AllocList}, term()


Each of these pids is a temporary decode pid, that dies after decode.  As soon as the PID dies, we clean our binary, life is great!

BUT if we use a part of this binary, say we do 

BinaryOver64Bytes = maps:get(description, JsonMap), mnesia:write({just_example, BinaryOver64Bytes , 5}

We have a huge problem now. This 8mb binary will NEVER get cleaned.  Even with a forced GC.  

My hypothesis is jiffy optimizes decoding the json and does not copy the terms over (binary:copy/1) when it creates the output map.

This means when we store a part of this binary permanently say into mnesia, we have the whole binary that never gets cleaned.

The solution is obvious, binary:copy/1 the terms that are permanently stored from the 8mb binary.

My question is, does erlang provide anything to see what is holding a ref to which binary? 
Also another question is should erlang make it so easy to shoot yourself in the foot here.

Would it work to optimize the shared binary garbage collection that it will consider which chunk of a binary is used, and discard the rest that is unused?

This is a common question/problem that arises and I already lost count of how many Erlang users start chats on this topic.

IMO It should not be this easy to screw up, especially with a language like erlang.  

More information about the erlang-questions mailing list