O'Reilly logo

Backbone.js Cookbook by Vadim Mirgorod

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Avoiding memory leaks in a Backbone application

A memory leak is a problem that can occur in a computer program due to incorrect memory allocation. In high-level object-oriented languages such as JavaScript, memory leak is often related to an object that is stored in the memory but isn't used by an application code. A memory leak can lead to a more serious problem such as exhausting the available system memory.

The following example demonstrates memory leak caused by a closure (anonymous function):

var div = document.createElement("div");
div.onclick = function () {  }

In the preceding code, a new HTML element is created and the onclick callback is assigned to an anonymous function. Such a code produces a memory leak because div references to a closure, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required