Memory management (Definition)

The process of organizing and keeping track of memory allocation and deallocation.

Memory management in compiled languages tends to be a primary concern of software developers. Because JavaScript is interpreted and is intended for use by designers as well as developers, a great deal of the complexity of memory management is hidden from view.

It is still possible however to design a script that will consume large amounts of memory due to what is called a memory leak.

A memory leak is when you allocate some storage and you don't subsequently relinquish it and make it available to the system again.

A prime example of a memory leak in the context of a JavaScript execution is the allocation of string data to String variables. When a new assignment is made, the old storage is unlinked from the variable and some new storage is allocated. This means that the storage management is far simpler because the strings tend to grow longer as new values are assigned. However, in a web browser, the discarded string values continue to sit around in memory until the page is refreshed. At that stage, all page local values are purged and the memory is freed.

You cannot force a garbage collection in JavaScript execution session in a web browser other than by setting the location.href of the current page to itself. This has the side effect of reloading the page from the web server, presenting the user with a possibly ugly transition artifact and increasing net traffic. However, this may be far more preferable than consuming 50 Megabytes of memory every few minutes in the client.

See also:Garbage collection, Memory leak