QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads JavaScript Global Variables And Memory Leakage

JavaScript Global Variables And Memory Leakage

One of the bad JavaScript programming practice is creating large number of global variables. In javascript dynamic memory(or objects) are removed from memory by the garbage collector when their variable scope ends or are dereferenced. Scope of global variable never ends therefore they remain in memory throughout the execution of the page even if they are not needed. This scenario creates memory leakage(unused objects remaining in memory).

There are basically two solutions to this problem: Use function scope or else dereference them manually.

JavaScript has no local scope. Local scope has been introduced in JavaScript 1.6 but thats not yet been implemented by any browsers yet. So use function scope to create variable so that variables are deleted when the function ends.

Instead of

var object1 = {};
var object2 = {};

Use

(function (window, document, undefined) {
    var object1 = {};
    var object2 = {};
})(window, document);

In the above example I used Immediately-Invoked Function Expression.

The second method is to dereference the objects manually therefore garbage collection deletes objects that are not referenced by any variables.

var object1 = {};
var object2 = {};

object1 = null;//object removed from memory
object2 = null;//object removed from memory

Here remember that object is removed from dynamic memory but the variables is still there on the stack i.e., pointing to the null object.

If you have too many global variables and you want to dereference by yourself then you can do this:

var array = [{}, {}];//create an array of objects

for(var x = 0; x < array.length; x++)
{
    array[x] = null;//clean all objects
}

“delete” operator

“delete” operator can only dereference object properties not variables(global or function scope) i.e., anything declared using “var” keyword cannot be dereferenced using “delete” operator.

Although we can access global variables using window object, they are not considered as properties of global object therefore we cannot deference them using “delete” operator.

Jul 21, 2014Narayan Prusty
Creating Cascading Grid Layouts Using MasonryJavaScript Function Techniques You May Not Know
Comments: 0

    Leave a Reply Cancel reply

    To create code blocks or other preformatted text, indent by four spaces:

        This will be displayed in a monospaced font. The first four
        spaces will be stripped off, but all other whitespace
        will be preserved.
        
        Markdown is turned off in code blocks:
         [This is not a link](http://example.com)
    

    To create not a block, but an inline code span, use backticks:

    Here is some inline `code`.

    For more help see http://daringfireball.net/projects/markdown/syntax

    Narayan Prusty

    I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

    8 years ago 1 Comment Web Development
    Share this
    0
    GooglePlus
    0
    Facebook
    0
    Twitter
    0
    Linkedin
    Related Articles
    • Difference between Set and WeakSet in JavaScript
    • Difference between “Map” and “WeakMap” in JavaScript
    • JavaScript Function Techniques You May Not Know
    • Creating JavaScript Modules
    • Creating A Viewport Resizer
    Our Sponsor
    My Books

    2014 - 2015 © QNimate
    All tutorials MIT license