Why/How 是我的代码导致了内存泄漏吗?
Why/How is my code causing a memory leak?
我在 Spotfire TextArea 中编写了以下 JavaScipt 代码。为了完整性,我包含了应用程序和标签,但我认为我的问题不是 Spotfire 特有的。本质上,我有一个每 5 分钟运行一次的计时器,并单击 link (clickLink('Foo');
) 以触发应用程序其他地方的某些 Python 代码的执行。如果应用程序还包含最后一次完整更新的时间戳,以相同的方式每 30 分钟发生一次 (clickLink('Foo');
):
function reportTimestamp() {
var timeNow = new Date();
var hoursNow = timeNow.getHours();
var minutesNow = timeNow.getMinutes();
var secondsNow = timeNow.getSeconds();
return hoursNow + ":" + minutesNow + ":" + secondsNow;
};
function timeBasedReaction(timestampAge){
if (timestampAge >= 1800) {
clickLink('Foo');
clickLink('Bar');
} else if (timestampAge >= 300) {
clickLink('Foo');
};
};
/*
function timeBasedReaction_B(timestampAge){
if (timestampAge >= 300) {
clickLink('Foo');
if (timestampAge >= 1800) {
clickLink('Bar');
};
};
};
*/
function clickLink(linkName) {
var clickTarget = document.getElementById(linkName).children[0];
clickTarget.click();
};
function checkTimestampAge() {
console.log(reportTimestamp());
var myTimeStamp = document.getElementById('Timestamp').children[0]
var timeStampMS = new Date(myTimeStamp.textContent).getTime();
var currentDateMS = new Date().getTime();
var timestampAgeSeconds = (currentDateMS - timeStampMS)/1000;
timeBasedReaction(timestampAgeSeconds);
};
function pageInitialization() {
checkTimestampAge();
var myTimer = null;
var timerInterval = 300000;
myTimer = setInterval(function(){checkTimestampAge()},timerInterval);
}
pageInitialization();
由于我不清楚的原因,运行 此代码在应用程序或 Web 浏览器中开始时运行良好,但最终会导致非常大的内存分配。
我试过阅读
4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them,
JS setInterval/setTimeout Tutorial,以及
An interesting kind of JavaScript memory leak,这是一个开始,但我还不够了解我做错了什么以及如何改正它。
谢谢,抱歉文字太长。
这会导致内存泄漏,因为 Spotfire 处理 Javascript 的方式已将 with/loaded 关联到 TextArea。
在桌面客户端以及 Webplayer 实例中,加载页面时,将加载该页面的所有部分,包括 TextArea 以及其中关联的 Javascript。我之前在上面评论中的理解:
"the code is intended to run when the page loads, and it was my understanding that it would stop/be cleared if the page was re-loaded or someone navigated away from it"
不正确。脚本的其中一项操作是 update/redraw TextArea 中的 HTML 位置。这反过来会重新加载 TextArea ,但不会清除现有的 Javascript 代码 。然而,它也不再真正可访问了,因为 var myTimer = null
实际上创建了一个新的 myTimer
而不是取消现有的。通过这种方式,myTimer
的实例随着 function timeBasedReaction
运行 的实例呈几何级数增长,并不断更新底层 TextArea 并加载更多相同的 Javascript.
对于遇到类似问题并来到这里的任何人,已经 3 个多月了,我还没有想出如何一劳永逸地解决这个问题。如果我这样做了,我会尝试再回来更新。
我在 Spotfire TextArea 中编写了以下 JavaScipt 代码。为了完整性,我包含了应用程序和标签,但我认为我的问题不是 Spotfire 特有的。本质上,我有一个每 5 分钟运行一次的计时器,并单击 link (clickLink('Foo');
) 以触发应用程序其他地方的某些 Python 代码的执行。如果应用程序还包含最后一次完整更新的时间戳,以相同的方式每 30 分钟发生一次 (clickLink('Foo');
):
function reportTimestamp() {
var timeNow = new Date();
var hoursNow = timeNow.getHours();
var minutesNow = timeNow.getMinutes();
var secondsNow = timeNow.getSeconds();
return hoursNow + ":" + minutesNow + ":" + secondsNow;
};
function timeBasedReaction(timestampAge){
if (timestampAge >= 1800) {
clickLink('Foo');
clickLink('Bar');
} else if (timestampAge >= 300) {
clickLink('Foo');
};
};
/*
function timeBasedReaction_B(timestampAge){
if (timestampAge >= 300) {
clickLink('Foo');
if (timestampAge >= 1800) {
clickLink('Bar');
};
};
};
*/
function clickLink(linkName) {
var clickTarget = document.getElementById(linkName).children[0];
clickTarget.click();
};
function checkTimestampAge() {
console.log(reportTimestamp());
var myTimeStamp = document.getElementById('Timestamp').children[0]
var timeStampMS = new Date(myTimeStamp.textContent).getTime();
var currentDateMS = new Date().getTime();
var timestampAgeSeconds = (currentDateMS - timeStampMS)/1000;
timeBasedReaction(timestampAgeSeconds);
};
function pageInitialization() {
checkTimestampAge();
var myTimer = null;
var timerInterval = 300000;
myTimer = setInterval(function(){checkTimestampAge()},timerInterval);
}
pageInitialization();
由于我不清楚的原因,运行 此代码在应用程序或 Web 浏览器中开始时运行良好,但最终会导致非常大的内存分配。 我试过阅读 4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them, JS setInterval/setTimeout Tutorial,以及 An interesting kind of JavaScript memory leak,这是一个开始,但我还不够了解我做错了什么以及如何改正它。
谢谢,抱歉文字太长。
这会导致内存泄漏,因为 Spotfire 处理 Javascript 的方式已将 with/loaded 关联到 TextArea。
在桌面客户端以及 Webplayer 实例中,加载页面时,将加载该页面的所有部分,包括 TextArea 以及其中关联的 Javascript。我之前在上面评论中的理解:
"the code is intended to run when the page loads, and it was my understanding that it would stop/be cleared if the page was re-loaded or someone navigated away from it"
不正确。脚本的其中一项操作是 update/redraw TextArea 中的 HTML 位置。这反过来会重新加载 TextArea ,但不会清除现有的 Javascript 代码 。然而,它也不再真正可访问了,因为 var myTimer = null
实际上创建了一个新的 myTimer
而不是取消现有的。通过这种方式,myTimer
的实例随着 function timeBasedReaction
运行 的实例呈几何级数增长,并不断更新底层 TextArea 并加载更多相同的 Javascript.
对于遇到类似问题并来到这里的任何人,已经 3 个多月了,我还没有想出如何一劳永逸地解决这个问题。如果我这样做了,我会尝试再回来更新。