These are chat archives for ractivejs/ractive

30th
Oct 2018
Cerem Cem ASLAN
@ceremcem
Oct 30 2018 17:50
@evs-chris what is your best practice while cleaning up unneeded objects in js in order to prevent memory leaks? this topic receives many approaches: https://stackoverflow.com/q/684575/1952991
I'm just wondering why there isn't a simple function like:
var cleanup, toString$ = {}.toString;
cleanup = function(o){
  var key;
  switch (toString$.call(o).slice(8, -1)) {
  case 'Array':
    o.length = 0;
    break;
  case 'Object':
    for (key in Object.getOwnPropertyNames(o)) {
      delete obj[key];
    }
    break;
  default:
    throw new Error("Unknown type: " + toString$.call(o).slice(8, -1));
  }
};
Chris Reeves
@evs-chris
Oct 30 2018 19:28
in what context? (app or ractive)
for my apps, if I need to evict an object, I just set it to null and if I need to just kill one key, I'll get the object and delete the key
ractive pretty much leaves that up to the user for data, but it tries to keep everything clean by making sure references don't hang around on nodes or in global caches
most js engines are pretty good about garbage collection, so as long as it's not referenced anywhere that will keep it hanging around, most garbage is safely collected
Joseph
@fskreuz
Oct 30 2018 20:20

I'm just wondering why there isn't a simple function

There are libraries that actually do this, pooling and recycling objects. It's mostly to avoid GC pauses. However it does add additional housekeeping in your code.

And modern GCs (I think Edge started it) now do parallel GC which no longer pause.
So it's much more easier to just ensure nothing is referenced, and just let the GC handle it.
Also helps to have a centralized data store (e.g. redux and friends).
Chris Reeves
@evs-chris
Oct 30 2018 20:22
I've looked into object pooling for a few things after doing some profiling and came to the conclusion that it's probably better to just let the gc do its thing
I'm a little confused about how well immutable stores interact re garbage though
seems like they would increase garbage excepting places where you want the same object in multiple places
Cerem Cem ASLAN
@ceremcem
Oct 30 2018 20:54

in what context? (app or ractive)

app

Cerem Cem ASLAN
@ceremcem
Oct 30 2018 21:00

However it does add additional housekeeping in your code.

I accept the additional burden of writing code in exchange for minimizing memory consumption (the memory allocated between gc runs), gc pauses and memory leaks

...however I don't believe I know the procedure well enough
Cerem Cem ASLAN
@ceremcem
Oct 30 2018 21:05

I just set it to null and if I need to just kill one key, I'll get the object and delete the key

I'll keep that in mind. thanks.

Cerem Cem ASLAN
@ceremcem
Oct 30 2018 21:14
@fskreuz yes, that's a good article. so "static memory javascript" approach requires not to rely on GC at all
Joseph
@fskreuz
Oct 30 2018 21:15
I'd like to think of it like as preventing the GC from kicking in. :smile: