Book Image

PHP Ajax Cookbook

Book Image

PHP Ajax Cookbook

Overview of this book

Table of Contents (16 chapters)
PHP Ajax Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Fixing memory leaks in JavaScript


Memory leaks can be one of the tedious problems in JavaScript, if JavaScript code is not written properly, considering memory usage. Such code might make your browser unstable by overloading memory.

What is a memory leak?

Memory leaks occur when memory allocated by JavaScript consumes physical memory but can't release the memory. JavaScript is a language that does garbage collection. Memory is assigned to an object when created and later on memory is returned once the object has no more references.

What might cause a memory leak?

There might be a lot of reasons behind memory leaks, but let's explore two major possibilities:

  • You're creating a lot of elements or JavaScript objects without cleaning up the unused ones.

  • You're using a circular reference in your JavaScript code. Circular reference occurs when a DOM object and JavaScript Object refer to each other circularly.