我玩HttpRequest并意识到在任何请求之后内存都没有被清除。 一段时间后,Chrome中的运行标签会崩溃。
这是一些测试代码。将大型文件放入“web”目录并相应地在代码中设置URL。
import 'dart:async';
import 'dart:html';
void main() {
const PATH = "http://127.0.0.1:3030/PATH_TO_FILE";
new Timer.periodic(new Duration(seconds:10), (Timer it)=>getString(PATH));
}
void getString( String url){
HttpRequest.getString(url).then((String data){
});
}
重新检查,内存泄漏仍然存在:
内存泄漏仅存在于Dartium中。当我将代码编译为JS并在Firefox中运行时,内存使用量将高达3.5 GB并保持不变。
这真的是一个错误还是我错了?
答案 0 :(得分:0)
还有另一个问题here,暗示HttpRequest中存在内存泄漏;但我无法在Dart问题跟踪器中找到任何内容。如果您认为这可能是真正的内存泄漏,则可能值raising a bug。
答案 1 :(得分:0)
存在问题,但已关闭 最近宣布的更改需要明确关闭请求,否则它将保持打开15秒(默认值)。
请参阅https://code.google.com/p/dart/issues/detail?id=20833上的讨论了解更多详情。
import 'dart:io';
void main(List<String> args) {
HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 9090).then((server) {
server.listen((HttpRequest request) {
var client = new HttpClient();
client.getUrl(Uri.parse("https://www.google.com")
.then((req) => req.close())
.then((resp) => resp.drain())
.whenComplete(() {
client.close();
request.response.close();
});
});
});
}
在像这样的代码中,有一个全局共享的HttpClient实例是正确的,因为它将处理共享持久连接。
import 'dart:io';
void main(List<String> args) {
HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 9090).then((server) {
server.listen((HttpRequest request) {
var client = new HttpClient();
client.getUrl(Uri.parse("https://www.google.com")
.then((req) => req.close())
.then((resp) => resp.drain())
.whenComplete(() => request.response.close());
});
});
}