简体   繁体   中英

Why/How is my code causing a memory leak?

I have written the following JavaScipt code within a Spotfire TextArea. I include the application and tag for completeness, but I don't believe my issue is Spotfire-specific. Essentially, I have a timer which runs every 5 minutes, and clicks on a link ( clickLink('Foo'); ) to trigger execution of some Python code elsewhere in the application. If the application also contains a timestamp of the last full update, which occurs every 30 minutes in the same manner ( 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();

For reasons unclear to me, running this code in the application or in a web browser starts off fine, but eventually leads to very large memory allocation. I've tried to read 4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them , JS setInterval/setTimeout Tutorial , and An interesting kind of JavaScript memory leak , and it's a start, but I don't know enough to really understand what I'm doing wrong and how to correct it.

Thanks, and sorry for the huge block of text.

This causes a memory leak because of how Spotfire handles Javascript which has been associated with/loaded into a TextArea.

Both in the desktop client, as well as in the Webplayer instance, when the page is loaded, all the portions of that page are loaded, include the TextArea and including the Javascript associated therein. My previous understanding in the comments above:

"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"

was incorrect. One of the script's actions was to update/redraw the HTML location in the TextArea. This, in turn, reloads the TextArea but does not clear the existing Javascript code . However, it's not really accessible anymore, either, since var myTimer = null actually creates a new myTimer rather than nulling-out the existing one. In this way, instances of myTimer increase geometrically as instances of function timeBasedReaction run and continually update the underlying TextArea and load in more of the same Javascript.

To anyone who ha a similar issue and come here, it's been over 3 months and I haven't figured out how to solve this once and for all. If I do, I'll try to come back with another update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM