Backbone-relational
Backbone-relational copied to clipboard
Store causing huge memory retention and cleanup takes forever
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:
- Perform a deep cleanup - Iterate through the whole
Tree
callingBackbone.Relational.store.unregister
on each model. This works but sometimes takes minutes to complete which is completely unacceptable from a user experience perspective. - Backup the
Store
before loading theTree
and restore it to its original state. I've also triedunregistering
all newCollections
inspired by theStore.reset
function. This approach seems to work unreliably and seems extremely hacky. - Introducing a new scope by using
addModelScope
andremoveModelScope
. This seems to have no affect and if my understanding is correct, themodel scope
is set only fordefinitions
and notinstances
.
Is there no graceful way to perform a memory cleanup?
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
.
@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.