sa125
sa125

Reputation: 28971

Continuous page refresh causes Firefox to increase memory consumption on windows

I have an odd situation with a webapp that keeps running down memory on Firefox / Windows. Basically the app refreshes the data in the page using a POST call to the server made via jQuery. Every time the call is made, Firefox's memory consumption increases in an amount that's disproportional to the size of the data returned from the server.

To see if this was specific to my app, I wrote a simple test app using Sinatra (Ruby 1.9.2-p318) and jQuery (1.7.1). The app sends a request to the server every 10 seconds and loads a 1MB html chunk to the page:

Server side:

require 'rubygems'
require 'sinatra'
require 'erb'
require 'json'

configure do
  set :static, true
end

post '/' do
  content_type :json

  # a simple html file containing ~ 1MB of data  
  html = File.read( File.join(File.dirname(__FILE__), 'html.txt' ) )

  # convert to JSON and return to the client
  return { "html" => html }.to_json
end

Client side:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>
  </head>
  <body>
    <h1>Test Page</h1>
    <div id="results" style="display: none;"></div>

    <script type="text/javascript">
      $(function(){
        // refresh the data every 10 sec
        setInterval( function(){ doRefresh(); }, 10 * 1000 );
      });

      function doRefresh() {
        $.post('/', function(data){
          $('#results').html( data.html );
          // attempt to free some memory
          delete data;
        }, 'json');
      }
    </script>
  </body>
</html>

What doesn't seem to change is that the memory consumption by the Firefox process (observed through Windows' Task Manager) keeps rising in 10's of megabytes with each call. Despite the fact that the new data replaces the old one in the page, it seems Firefox isn't disposing of that allocated space in memory. Turns out this runs down the memory completely if the page is left open overnight (on simple, 4GB machines).

Is this a javascript issue or something with Firefox? Can I somehow force garbage collection in either? thanks.

EDIT: This memory issue wasn't observed with Google Chrome (13.0.782.112 on Win7).

Upvotes: 1

Views: 1059

Answers (1)

Rodolphe BELOUIN
Rodolphe BELOUIN

Reputation: 214

If your 'data' argument has been instantiated with the 'new' keyword by jQuery, you should write this code :

…
$('#results').html( data.html );
delete data;
…

If deleting the data variable returns false. I think you can't do anything.

Upvotes: 1

Related Questions