我有一个奇怪的情况,一个 web 应用程序在 Firefox / Windows 上不断耗尽内存。基本上,该应用程序使用通过对服务器进行的POST调用来刷新页面中的数据jQuery
。每次调用时,Firefox 的内存消耗都会增加,这与服务器返回的数据大小不成比例。
为了查看这是否特定于我的应用程序,我使用 Sinatra (Ruby 1.9.2-p318) 和 jQuery (1.7.1) 编写了一个简单的测试应用程序。应用程序每 10 秒向服务器发送一个请求,并将 1MB 的 html 块加载到页面:
服务器端:
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
客户端:
<!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>
似乎没有改变的是 Firefox 进程的内存消耗(通过 Windows 的任务管理器观察到)在每次调用时不断增加 10 兆字节。尽管新数据替换了页面中的旧数据,但 Firefox 似乎并没有处理内存中分配的空间。事实证明,如果页面在一夜之间打开(在简单的 4GB 机器上),这会完全耗尽内存。
这是javascript问题还是Firefox的问题?我可以以某种方式强制垃圾收集吗?谢谢。
编辑:谷歌浏览器(Win7 上的 13.0.782.112)没有观察到这个内存问题。