Releasing memory

If I make a JSON call with a callback function that receives the data, will the called back function release the data returned once the function is finished? Is there a way to ask the garbage collector to run now?!
  • As long as you don't store the data in a class or global variable, function parameters will be freed once the function exits. You can also free a variable's memory by setting it null. Both of these will only happen though as long as the memory isn't referenced by another variable.

    var a = new SomeObject();
    a = null; // memory is freed

    a = new SomeObject();
    b = a;
    a = null; // memory is not freed, as b still has a reference to it
  • What if I do this...

    var city;

    function onReceive(responseCode, data)
    {
    var message = data["message"];
    city = data["name"];
    }

    will data never be freed?
  • Former Member
    Former Member over 10 years ago
    What if I do this...

    var city;

    function onReceive(responseCode, data)
    {
    var message = data["message"];
    city = data["name"];
    }

    will data never be freed?


    In this example, city is a global. responseCode, data, and message are local to the onReceive function.

    As soon as the onReceive function returns, the local variables (responseCode, data, and message) go out of scope and drop their references to the data they contain. city still contains a reference to the value that was associated with the "name" key in the data Dictionary. Whatever data was stored in that value will still have a reference held by city. The rest of the data that makes up the data Dictionary will no longer have any references and will be destroyed.
  • In this example, city is a global. responseCode, data, and message are local to the onReceive function.

    As soon as the onReceive function returns, the local variables (responseCode, data, and message) go out of scope and drop their references to the data they contain. city still contains a reference to the value that was associated with the "name" key in the data Dictionary. Whatever data was stored in that value will still have a reference held by city. The rest of the data that makes up the data Dictionary will no longer have any references and will be destroyed.


    Awesome thanks!