how to free memory from calling Bangle.buzz ?

Posted on
  • Hi,

    in my game when i call Bangle.buzz free memory keeps decreasing until it runs out of memory. I noticed in the documentation it says it returns a promise but i'm not familiar with them and i don't know how i should use Bangle.buzz so it does not keep eating free memoy or how to free the memory from probably the returned promise.

    for example when testing i tried this code and it runs out of memory when doing this resulting in an error:

    var memStart;
      memStart = process.memory(true);
    
    for(i = 0; i < 1000; i++)
    {
      Bangle.buzz(1,1);
      
    var memTmp = process.memory(true);
    var used = memTmp.usage - memStart.usage;
      print(i.toString() + ": Udiff:"+ used.toString() + " used:" + memTmp.usage.toString() + " free:" + memTmp.free.toString() + " total:" + memTmp.total.toString()  );
    }
    
    for(i = 0; i < 1000; i++)
    {
       Bangle.buzz(1,1);
    var memTmp = process.memory(true);
    var used = memTmp.usage - memStart.usage;
      print(i.toString() + ": Udiff:"+ used.toString() + " used:" + memTmp.usage.toString() + " free:" + memTmp.free.toString() + " total:" + memTmp.total.toString()  );
    }
    
    for(i = 0; i < 1000; i++)
    {
      Bangle.buzz(1,1);
    var memTmp = process.memory(true);
    var used = memTmp.usage - memStart.usage;
      print(i.toString() + ": Udiff:"+ used.toString() + " used:" + memTmp.usage.toString() + " free:" + memTmp.free.toString() + " total:" + memTmp.total.toString()  );
    }
    

    I'm not sure if i'm doing something wrong here but it shows something like this as a result

    ...
    ...
    369: Udiff:11929 used:11972 free:28 total:12000
    370: Udiff:11934 used:11977 free:23 total:12000
    371: Udiff:11939 used:11982 free:18 total:12000
    Execution Interrupted
    at line 4 col 33
    var memTmp = process.memory(true);
                                    ^
    New interpreter error: LOW_MEMORY,MEMORY
    >
    > 
    
  • Could it be that your loops are keeping the CPU so busy that it can not run the garbage collector to cleanup the memory?

    Try the following: Wait a bit, do nothing and check the memory again. Or use setTimeout/Intervall() to trigger buzz and memory logging.

  • Hi,

    thanks for your answer, the example functions above are indeed not good, but in my game the game is idle if no input has happened and it never garbage collects, however i now found out this issue only appears in the emulator in the browser on the pc, when i tested it on the device itself with my game it works fine but somehow in the espruino ide and emulator using same code from my game when calling bangle.buzz never gets freed and eventually when enough buzzes had past even with very long delays between input and code execution it never garbage collects it.

    I should have tested on the device first but i was mainly developping in the ide with the emulator as it works a bit faster.

    I still don't know why the emulator does not free the memory. but i have created a better example code:

    each time you do a touch on the screen memory keeps increasing and never gets released in the emulator, i did touches with pauses in between until memory was exhausted and it did not garbage collect. On the device itself it actually behaves fine and memory remains stable but in the emulator in my chrome browser it does not and keeps increasing until enough buzzes have been done for the memory to be exhausted

    if you comment bangle.buzz the issue never happens

    var memStart;
      memStart = process.memory(true);
    
    function doBuzzer(e)
    {
      Bangle.buzz(1,1);
    
      var memTmp = process.memory(true);
      var used = memTmp.usage - memStart.usage;
      print(": Udiff:"+ used.toString() + " used:" + memTmp.usage.toString() + " free:" + memTmp.free.toString() + " total:" + memTmp.total.toString()  );
    }
    
    
    Bangle.on("touch", doBuzzer);
    
  • Looks fine to me:

    : Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000
    Udiff:76 used:1347 free:10653 total:12000

  • ah good to know it does not happen with you in the emulator (on pc / browser)

    this is what i get and it never decreases but on the bangle itself it works fine

    : Udiff:68 used:111 free:11889 total:12000
     Udiff:78 used:121 free:11879 total:12000
     Udiff:85 used:128 free:11872 total:12000
     Udiff:90 used:133 free:11867 total:12000
     Udiff:95 used:138 free:11862 total:12000
     Udiff:100 used:143 free:11857 total:12000
     Udiff:105 used:148 free:11852 total:12000
     Udiff:110 used:153 free:11847 total:12000
     Udiff:115 used:158 free:11842 total:12000
     Udiff:120 used:163 free:11837 total:12000
     Udiff:125 used:168 free:11832 total:12000
     Udiff:130 used:173 free:11827 total:12000
     Udiff:135 used:178 free:11822 total:12000
     Udiff:140 used:183 free:11817 total:12000
     Udiff:145 used:188 free:11812 total:12000
     Udiff:150 used:193 free:11807 total:12000
     Udiff:155 used:198 free:11802 total:12000
     Udiff:160 used:203 free:11797 total:12000
     Udiff:165 used:208 free:11792 total:12000
     Udiff:170 used:213 free:11787 total:12000
     Udiff:175 used:218 free:11782 total:12000
     Udiff:180 used:223 free:11777 total:12000
    

    I'll see if i can pinpoint something on my pc / browser that can affect this, just to be sure you ran the code in the emulator or on the bangle itself ? the emulator is where i seem to have the issue

  • ah sorry, no this is on the bangle. Sounds like an emulator bug.

  • yeah indeed, on the bangle it works fine for me also i was not aware of that initially, so that makes me less worried. I should test more on the bangle also when running in such things. On the bangle memory remains the same as well for me but not in the emulator. I have not tried a different browser yet. I even went as far and "touched" a 1000 times to see if it would eventually crash in the browser using the emulator and it did, but on the bangle its indeed fine which is a relieve

  • Ahh, thanks - yes, this looks like an emulator bug.

    I guess because the emulator can't 'Buzz' it's not implemented, so a promise gets made and returns but it is never resolved and stays in memory

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

how to free memory from calling Bangle.buzz ?

Posted by Avatar for joyrider3774 @joyrider3774

Actions