如何同步AJAX调用可能导致内存泄漏? [英] How synchronous AJAX call could cause memory leak?

查看:425
本文介绍了如何同步AJAX调用可能导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解<一href="http://stackoverflow.com/questions/2088318/is-there-any-reason-to-use-a-synchronous-xmlhtt$p$pquest/3144550#3144550">this一般建议给予反对使用同步AJAX调用,因为同步调用阻塞UI渲染。

I understand this general advice given against the use of synchronous ajax calls, because the synchronous calls block the UI rendering.

通常给出的另一个原因是内存泄漏isssues是同步 AJAX。

The other reason generally given is memory leak isssues with synchronous AJAX.

在<一个href="https://developer.mozilla.org/en-US/docs/DOM/XMLHtt$p$pquest/Synchronous_and_Asynchronous_Requests">MDN文档 -

请注意:你不应该使用同步XMLHtt prequests因为,由于   联网的固有异步性质,有各种   记忆方法和事件可以使用同步请求时泄漏。该   唯一例外的是,同步请求工人里面工作。

Note: You shouldn't use synchronous XMLHttpRequests because, due to the inherently asynchronous nature of networking, there are various ways memory and events can leak when using synchronous requests. The only exception is that synchronous requests work well inside Workers.

如何同步调用可能导致内存泄漏?

How synchronous calls could cause memory leaks?

我要寻找一个实际的例子。 任何指针关于这一主题的宣传材料将是巨大的。

I am looking for a practical example. Any pointers to any literature on this topic would be great.

推荐答案

如果XHR正确实施的按规格,那么就不会泄露:

If XHR is implemented correctly per spec, then it will not leak:

这是XMLHtt prequest对象不能被垃圾收集,如果其状态是   OPENED和发送()标志被置位,它的状态是HEADERS_RECEIVED,或   它的状态是LOADING,和以下条件之一为真:

An XMLHttpRequest object must not be garbage collected if its state is OPENED and the send() flag is set, its state is HEADERS_RECEIVED, or its state is LOADING, and one of the following is true:

它有一个或多个事件监听器注册,其类型为   readystatechange,进度,中止,有错误,负荷,超时或loadend。

It has one or more event listeners registered whose type is readystatechange, progress, abort, error, load, timeout, or loadend.

上传完成标志没有设置和相关的   XMLHtt prequestUpload对象注册了一个或多个事件侦听器   其类型是一种进步,中止,有错误,负荷,超时或loadend。

The upload complete flag is unset and the associated XMLHttpRequestUpload object has one or more event listeners registered whose type is progress, abort, error, load, timeout, or loadend.

如果一个XMLHtt prequest对象被垃圾回收,而其连接   仍处于打开状态,用户代理必须取消的获取任何实例   此对象的算法打开,丢弃任何任务排队对他们来说,   和废弃从网络接收的任何进一步的数据为它们

If an XMLHttpRequest object is garbage collected while its connection is still open, the user agent must cancel any instance of the fetch algorithm opened by this object, discarding any tasks queued for them, and discarding any further data received from the network for them.

所以,你打后。发送() XHR对象(和任何它引用)成为免疫GC。但是,任何错误或成功将使XHR成DONE状态并成为受到GC了。它不会在所有问题,如果XHR对象是同步或异步。如果长同步请求再次没关系,因为你也只是停留在发送的语句,直到服务器响应。

So after you hit .send() the XHR object (and anything it references) becomes immune to GC. However, any error or success will put the XHR into DONE state and it becomes subject to GC again. It wouldn't matter at all if the XHR object is sync or async. In case of a long sync request again it doesn't matter because you would just be stuck on the send statement until the server responds.

然而,根据<一href="https://docs.google.com/$p$psentation/d/1OFG81taxgjOGU43sv9WHvPZkt5--KnM6gSijWN8NMcU/edit?disco=AAAAAECHbXY&pli=1#slide=id.g16a8b8ed_0_140">this幻灯片它没有被正确执行,至少在铬/铬在2012年每规范,就没有必要再调用 .abort()自DONE状态意味着XHR对象应该已经正常GCD。

However, according to this slide it was not implemented correctly at least in Chrome/Chromium in 2012. Per spec, there would be no need to call .abort() since the DONE state means that the XHR object should already be normally GCd.

我找不到甚至丝毫证据来支持的MDN声明,我所接触过微博的作者。

I cannot find even slightest evidence to back up the MDN statement and I have contacted the author through twitter.

这篇关于如何同步AJAX调用可能导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆