2

I am very confused about Garbage collection in javascript and I am not sure when it happens.

I was wondering if all the garbage would be collected on each page change. So it might be worth while making a multipage app so that the garbage got collected regularly.

I am particularly interested because of the Mobile Safari image resource limit

This issue

or here is another one

So I am curious if I have a multipage app if this would make the image resource limit go away.

Community
  • 1
  • 1
Leo
  • 1,495
  • 23
  • 41
  • You can check https://taco.visualstudio.com/en-us/docs/better-web-performance/ Basically, having several pages means you'll clean everything from the previous page and you'll be sure you won't end up leaking memory too long. On the bad side, you'll always end up reloading the entire page (reloading your JS, recreating all DOM elements, ...). For your `image resource limit` issue, maybe you'd want to use a virtual list, ie only render the images that are visible. – user276648 Sep 14 '16 at 00:50

2 Answers2

1

So far I've seen and learnt that Phonegap doesn't have any garbage collection mechanism by it's own. It totally depends on how platforms handle their web views. Also javascript garbage collection is handled by the browsers differently.

So one browser may handle it in a way, another browser may not. I would also like to know if someone answers about garbage collection for multi-page. As of now I think it's varying on platforms.

As far as I know ios will only revoke garbage collection when it need to. It used ARC(Automatic Reference Counting) On the other hand android has native code to force garbage collection like System.gc() So you may call native code from JS when you need garbage collection in android. Can't tell you what to do for ios. You can check the platform based garbage collection that might help you to know a bit more.

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
1

Not an expert on the subject, but here's what I have found:

This excellent Smashing Magazine article about garbage collection: Writing fast, memory-efficient Javascript (right above the "Rules of Thumb" subtitle) mentions:

"Globals are cleaned up when you refresh the page, navigate to a different page, close tabs or exit your browser. Function-scoped variables get cleaned up when a variable falls out of scope. When functions have exited and there aren’t any more references to it, the variable gets cleaned up." - SmashingMagazine Article

So yes, it is collected on each page change, however:

[...] "but test it in a real module of code or in an actual application, and the true impact of those optimizations may be much more minimal than you were expecting." - SmashingMagazine Article

Your app will thus probably suffer more in performance by having to load different resources than by letting the browser automatically collect garbage (unless you have a terrible coding style, that is)

The article has a nice example on how every type of object (variable, function, closure) is garbage collected.

Furthermore, this article entitled Why mobile web apps are slow is a very long (but great) explanation about how Garbage collection is actually a bad thing on mobile, because it actually slows down an app unless it disposes of 6x the amount of required memory (hence why it is considered a welcome help on desk- & laptop)

Further references:

Community
  • 1
  • 1
webketje
  • 10,376
  • 3
  • 25
  • 54