Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Doing swapping characters of strings tasks takes up tons of memory

I need to replace around 225 characters in multiple strings, then some additional work.

case 'k':
      console.log(`MEMORYA: JS: ${memory.js.used} / ${memory.js.total}`)
      for(let i=0; i<15;i++){
        for(let f=0; f<15;f++){
          canvas[(y-7)+i] = canvas[(y-7)+i].substring(0, (x-7)+f) + evt.data.data[i][f] + canvas[(y-7)+i].substring((x-7)+f+1);
        }
      }
      console.log(`MEMORYB: JS: ${memory.js.used} / ${memory.js.total}`)
      loadSection.style.display = "none";
      loadSection2.style.display = 'none'
      pixelsSection.style.display = 'inline'
      console.log(`MEMORYC: JS: ${memory.js.used} / ${memory.js.total}`)
      draw()
      console.log(`MEMORYD: JS: ${memory.js.used} / ${memory.js.total}`)
      canMove = true
      position.text = `${x}, ${y}`
      isRefresh = false
      console.log(`MEMORYE: JS: ${memory.js.used} / ${memory.js.total}`)
      break;

Output is as such:

[18:40:53]MEMORYA: JS: 36712 / 131064
[18:40:53]MEMORYB: JS: 59992 / 131064
[18:40:53]MEMORYC: JS: 60088 / 131064
[18:40:53]MEMORYD: JS: 52576 / 131064
[18:40:53]MEMORYE: JS: 52584 / 131064

memory jumps some 10,000 and never goes back down.

Any possible solutions? Been stuck on this for days.

What I've tried:

  • Using arrays (don't get me started)
  • Splitting, changing and joining (goes unresponsive, too slow)
  • Other ways of replacing strings
Best Answer
0 Votes
2 REPLIES 2

JavaScript (and most recent languages) don't usually release memory as soon as they can. They release it when the need to, which can mean just before a new memory allocation that can't be satisfied. Keep running your app and you should see that happening. Only panic if you actually get an out-of-memory error.

Peter McLennan
Gondwana Software
Best Answer

The thing is, it needs to happen a lot, up to once every 5 seconds or so.

After about a minute of interacting with the app, I get an out-of-memory error.

Best Answer
0 Votes