Backbone-relational icon indicating copy to clipboard operation
Backbone-relational copied to clipboard

Store causing huge memory retention and cleanup takes forever

Open anshul-kai opened this issue 8 years ago • 2 comments

I have an extremely large application (over 40k lines of code) which heavily relies on backbone relational. Using backbone relational has certainly come in handy in many regards but we are soon realizing that there seems to be a huge price to pay for large applications. I'm writing this with the hopes that my realization is wrong.

One of our bigger Models often ends up in a Tree structure with over 10k child nodes. This Tree has more than 10 levels of nestings with each child node representing a unique Model or a Collection. The problem at hand is a major memory retention that happens due to the Store.

Following are the approaches I've tried with results noticed:

  1. Perform a deep cleanup - Iterate through the whole Tree calling Backbone.Relational.store.unregister on each model. This works but sometimes takes minutes to complete which is completely unacceptable from a user experience perspective.
  2. Backup the Store before loading the Tree and restore it to its original state. I've also tried unregistering all new Collections inspired by the Store.reset function. This approach seems to work unreliably and seems extremely hacky.
  3. Introducing a new scope by using addModelScope and removeModelScope. This seems to have no affect and if my understanding is correct, the model scope is set only for definitions and not instances.

Is there no graceful way to perform a memory cleanup?

anshul-kai avatar Jul 07 '16 01:07 anshul-kai

This doesn't solve your problem, but I had the same issue.

Backbone relational started off as a nice feature that sped up development, but quickly became a bog on performance. Over the last couple of years I've migrated all my collections and models to my own plugin that helps manage relations between collections and models.

Check out Backbone Child Collections

Child collections/models are not instantiated until accessed and even then remain lightweight by not creating a bunch of event bindings

Children can be accessed via get(key) just like attributes and children have access to the parent via collection or parentModel.

kjantzer avatar Sep 25 '17 21:09 kjantzer

@a-koka I know this is an old issue, but one thing that REALLY helped for us, was to set the "includeInJSON" attribute of the reverseRelation to false. It is default by true, which was causing a super crazy large nested object as we used the toJSON method when setting up data for our views.

We went from 2.5GB of RAM for this one larger page, to 30MB.... that's how crazy the nested-ness can get.

nightsurge avatar Oct 11 '19 18:10 nightsurge