• I am attempting to build my ultimate multiclock style watch which is a combination of 3 of my clocks, namely "stepo", "waypointer" and "walkers clock". Each uses an arrayBuffer for the display so I need to be careful not to have any memory leaks or bad code.

    Each watch face will have an init() function to create any resources and a freeResource() function to set all the variables to undefined.

    (() => {
      function getFace(){
        var buf1;
        // desclare variables
    
        function init() {
          buf1 = Graphics.createArrayBuffer(160,160,1, {msb:true});
          // etc
        }
    
        function freeResources() {
          buf1 = undefined;
          // etc
        }
    
        // other functions as required
        return {init:init, freeResources:freeResources, startTimer:startTimer, stopTimer:stopTimer,
                onButtonShort:onButtonShort, onButtonLong:onButtonLong};
      }
      return getFace;
    })();
    
    

    When switching watch faces...

    function stopdraw() {
      face.stopTimer();
      face.freeResources();
    }
    
    function startdraw() {
      Bangle.drawWidgets();
      face.init();
      face.startTimer();
    }
    
    function setButtons(){
    
      function nextFace(){
        iface += 1
        iface = iface % FACES.length;
        stopdraw();
        face = FACES[iface]();
        g.clear();
        g.reset();
        startdraw();
      }
    

    Everything works fine for the first cycle through the watch faces. But within a few minutes I get a RED MEMORY_LOW warning at the bottom of the watch face. All the individual Apps work without memory problems.

    Anyone any suggestions on if the approach should work and whre to look for memory leaks.

    Hugh

  • Tue 2021.03.09

    Hi Hugh,

    Has an attempt to review the amount of 'Free' memory been tracked just after your init() functions to determine whether resources from memory are released after each task?

    Maybe try printing inside a setInterval() and monitor?

    http://www.espruino.com/Reference#l_procĀ­ess_memory


    process.memory();
    
    process.memory().free;
    

    I also noticed the use of timers. Any chance one is still running, say for instance that keeps
    creating objects? Snippet below that may be entered into L-Hand WebIDE console to view:

    console.log(global["\xFF"].timers); //nice, less info
    
    trace(global["\xFF"].timers); //full info
    
About

Avatar for Robin @Robin started