Memory Leak When Pulling JSON from WEB

后端 未结 3 809
遇见更好的自我
遇见更好的自我 2020-12-20 02:57

I\'ve spent days on this and hit it from every angle I can think of. I\'m working on a simple windows 7 gadget. This script will pull JSON data from a remote web server an

3条回答
  •  不知归路
    2020-12-20 03:15

    I have the feeling that the setTimeout function within the updateView is causing this behaviour. To test this you can modify your code to:

    $(document).ready(function () {
       setInterval(updateView, 1000);
    });
    
    function updateView(){
        $("#junk").html(count);
        count++;
        $.getJSON( URL + "&callback=?", populateView);
    }
    
    function populateView(status) {
        $("#debug").html(status.queue.mbleft + " MB Remaining
    " + status.queue.mb + " MB Total"); }

    EDIT: The setInterval function will execute the passed in function over and over every x miliseconds. Here to the docs.

    EDIT 2: Another performance loose (Although it might not be critical to the issue) is that you are traversing the DOM every second to find the $('#debug') element. You could store that and pass it in as:

            $(document).ready(function () {
                var debug = $('#debug'); 
                var junk = $('#junk')          ;
                setInterval(function(){updateView(debug, junk)}, 1000);
    
            });
    
            function updateView(debug, junk){
               junk.html(count);
                count++;
                $.getJSON( URL + "&callback=?", function(status){populateView(status,debug)});
            }
    
            function populateView(status) {
                debug.html(status.queue.mbleft + " MB Remaining
    " + status.queue.mb + " MB Total"); }

    Edit 3: I have changed the code above because I forgot to take in the response from the server. Assuming that queue is a property of the returned JSON then the code should be as above.

    Edit 4: This is a very interesting issue. Another approach then. Lets assume then that there is still some client side scripts that are clogging the memory. What could this be? As far as is I understand the only two things left are the setInterval and the $.getJSON function. The $.getJSON function is a simple ajax request wrapper which fires a request and waits for the response from the server. The setInterval function is a bit more peculiar one because it will set up timers, fire functions, etc.

    I think if you manage to mimic this on your server or even just refresh this webpage in your browser every second/5 secs you you will be able to see whether it is the client or the server that processes your request.

提交回复
热议问题