anyway, it just looks stupid that the garbage collector have that zigzag pattern at all
what memory does it allocate ? it uses one global variable and that's all
I didn't even declare a new variable.
This is strange indeed
1. garbage source.
While your example program itself does not visibly generate garbage - there still are plenty of garbage sources, a'la: virtual machine interpreting the javascript, jit compilation, whatever other internal structs it needs to implement the used or assumed to be used functionality of javascript.
Case in point of a possible source: calling a function needs a local scope/closure to be created and destroyed (technically - a good jit optimizer under specific conditions can prevent it from using generic GC for cleanup).
2. zigzag pattern.
Garbage collection is an overhead (ie. in a sense no useful work is done - GC is hardly the goal of any program
). Hence GC implementations avoid actually doing it if they do not need to do it (ie. there is plenty of free memory) - allowing memory usage to steadily grow till it finally decides to do the work causing a sudden drop in memory usage => zigzag.
edit:
it seems to leak according to chrome dev tools
I have not used chrome dev tools, but i guess you are misinterpreting what it tells you.
I don't think this is a case of circular dependencies or anything to do with DOM
Yep. However, memory is not free till GC tells so => every unused crumb of memory piles up over time till GC gets rid of them all, all at once.
is this normal in javascript ?
Yes. It is common in all languages that heavily use mark-and-sweep style GC, not just javascript.
One might have a leak problem if memory usage grows over GC cycles (ie zigzag bottom line keeps growing).