Eliminate circular reference memory-leak using weaken

The data structures of Perl are very flexible which means it is easy to
create circular reference.
Unfortunately when these data structures go out of scope the
reference-counting garbage collector of Perl won't clean up the mess.

Unless we explicitly weaken one of the links.

Example with memory leak

Each hash has a value referring to the other hash. When the function
finishes and the variables %x and %y go out of scope,
they won't be destroyed and removed from memory as each one still
has a reference leading to it.

If you read the other article
then you already know, the diff column
indicates the change in memory consumption, and when it grows too much, that's not a good sign.

Of course your application might not call the problematic function 10,000 times, and you
might be ok with an extra 15% memory usage for a short-lived script, but for bigger
applications, especially ones that need to run for a long time, you'll try to
eliminate every memory leak.

BTW I checked, and I already saw the symptoms after 380 calls.

Check the weakened version

Now if we remove the # characters, enable the weaken function call,
and run the script again the result is: