Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Memory leak in CanJS template/model #1393
When passing observables obtained from can.Model.findAll() or can.Model.find() into a can.view.mustache template generator, it appears to leak memory. The leak goes away when you call myobservable.attr() instead of passing the myobservable object directly to the template generator function. It doesn't matter whether the data comes from a real AJAX call or is provided by a fixture.
Here is a simple test to reproduce the issue (one button causes memory to leak, and the other doesn't - It just rerenders a template many times):
Note that you need to replace the all dependencies with their appropriate paths on your system.
Thanks for posting this.
Can you simplify it a bit more? It's probably possible to create this situation without can.Model or can.fixture.
I'm asking because I can't follow the code immediately. I would have to setup this test, which would take some time, and really dive in.
If I can follow the code, it's possible I can immediately spot the problem and advise on a fix.
@justinbmeyer Note sure, I haven't tried with stache yet.
@Tarabyte On my machine, the script I provided only leaks when you click on the 'Start test (with leak)' button.
When checking for memory leaks, I tend to check in complete isolation in a fresh tab - I'm not sure how jsfiddle interacts with all this code - It might skew results... I also noticed that jsfiddle runs the code in an iframe which can affect things too - For example, you will find that deleting the iframe cleans up all memory leaks (because iframe memory is sandboxed).
@justinbmeyer I will try to reproduce it using a simple Observe instead of the Model + Fixture combination - It might still leak.
Ultimately, something appears to be holding on to references of the data. When I was debugging a while ago, I think I saw something about a _cache property somewhere in 'can' and I remember thinking that this could be the cause... Does that ring a bell?
Yes, the model layer keeps instances, but only while they are bound. Clearing the page should clean them up.
Sent from my iPhone