0

after a ajax requst can you then free the memory by doing this?

function Ajax(){
    this.url = null;
    this.data = null;
    this.success = null;

    this.global = true;
    this.timeout = JSON_TIMEOUT;
    this.cache = false;
    this.dataType = 'json';
    this.type = 'post';

    var _this = this;

    this.send = function(){
        var jqxhr = $.ajax({
                url : this.url,
                data : this.data,
                timeout : this.timeout,
                cache : this.cache,
                dataType : this.dataType,
                type : this.type,
                global : this.global
                }
            )
            .success(this.success)
            .error(function(){
                Dialog.set_error({
                    headline : Lang.get('HDL_ERROR'),
                    body : Lang.get('ERR_TIMEOUT'),
                    btns : [
                        {
                            value : Lang.get('BTN_OK'),
                            script : function(){},
                            focus : true
                            }
                        ]
                    })
                })
            .complete(function(){
                    delete _this;
                });
    };
}
clarkk
  • 27,151
  • 72
  • 200
  • 340

2 Answers2

4

No. delete doesn't release memory, it removes properties from objects. If you call it on a var (as you've done), it has no effect.

JavaScript is a garbage-collected language. The memory consumed by an object is automatically reclaimed when nothing has a reference to that object anymore. Circular references are handled automatically (so even if A refers to B and B refers to A, provided nothing else refers to A or B, they can both be reclaimed).

Where you run into issues is circular references with non-JavaScript objects, on some browsers (I'm looking at you, Microsoft). If you have a DOM element that refers to an object (say, a function), and that object also refers to the DOM element, on IE even if nothing else refers to either of those objects, they'll never get reclaimed. But that wouldn't seem to be the case with your example.

(Re my statement about delete above: If it happens that the property you're removing from the object refers to an object that nothing else refers to, then removing the property makes the object available to be reclaimed. But that's a side effect, and again, delete only relates to properties, not variables.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

JavaScript runs its own garbage collector. That means you don't need (nor you can) to do anything about it.

This answer deepen your curiosity.

Community
  • 1
  • 1
Luca Fagioli
  • 12,722
  • 5
  • 59
  • 57