Question about Out of memory

Posted on
  • Hi!
    I have tried to use Puck.IR to control my LED Light strip.
    After a few days of mining I have collected commands from remote control.
    But when I have tried to write them to Puck, I have Out of memory.

    After I have removed few commands it starts working. But nearly full memory is used.
    Is it correct? How can I reduce memory usage?
    Working version looks like this:

    
    var cmd={
    On:    [8.97,4.42,0.58,0.52,0.61,0.52,0.58,0.55,0.58,0.52,0.61,0.52,0.61,0.49,0.61,0.52,0.58,0.52,0.64,1.59,0.64,1.62,0.58,1.65,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.61,1.62,0.58,0.52,0.61,0.52,0.61,0.52,0.58,0.52,0.61,0.52,0.58,0.52,0.61,0.52,0.58,0.52,0.64,1.59,0.64,1.62,0.58,1.65,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.58,1.65,0.55],
    Down:  [8.97,4.42,0.58,0.52,0.61,0.52,0.58,0.55,0.58,0.52,0.61,0.52,0.61,0.52,0.58,0.52,0.58,0.52,0.64,1.59,0.64,1.62,0.58,1.65,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.61,1.62,0.58,0.52,0.61,0.52,0.61,1.62,0.61,1.65,0.58,0.52,0.61,0.49,0.64,0.49,0.58,0.52,0.61,1.65,0.58,1.65,0.61,0.52,0.58,0.52,0.61,1.65,0.58,1.65,0.61,1.62,0.61,1.62,0.58],
    Up:    [9.00,4.39,0.61,0.49,0.64,0.49,0.61,0.52,0.61,0.49,0.64,0.49,0.58,0.52,0.64,0.49,0.61,0.52,0.58,1.65,0.61,1.62,0.61,1.68,0.55,1.65,0.61,1.62,0.58,1.65,0.58,1.65,0.61,1.62,0.61,1.65,0.58,0.52,0.61,0.49,0.64,1.62,0.61,0.49,0.61,0.52,0.58,0.52,0.61,0.52,0.58,0.52,0.61,1.62,0.64,1.62,0.58,0.52,0.64,1.62,0.58,1.65,0.58,1.65,0.61,1.62,0.61],
    Set25: [9.00,4.39,0.61,0.49,0.61,0.52,0.58,0.52,0.61,0.52,0.58,0.55,0.58,0.52,0.61,0.52,0.58,0.52,0.61,1.62,0.61,1.65,0.61,1.62,0.58,1.65,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.58,0.52,0.61,1.62,0.58,0.55,0.58,1.65,0.58,0.55,0.58,0.52,0.55,0.55,0.61,0.52,0.64,1.59,0.61,0.52,0.61,1.62,0.58,0.55,0.61,1.62,0.58,1.65,0.61,1.62,0.61,1.65,0.58],
    Set50: [9.00,4.39,0.61,0.49,0.64,0.49,0.58,0.52,0.64,0.49,0.64,0.49,0.58,0.52,0.61,0.52,0.61,0.52,0.58,1.62,0.64,1.62,0.61,1.62,0.67,1.56,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.61,1.62,0.55,0.55,0.64,1.62,0.58,1.65,0.58,0.55,0.58,0.52,0.61,0.49,0.64,0.49,0.61,0.52,0.61,1.62,0.58,0.52,0.64,0.49,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.61],
    Set75: [9.00,4.39,0.61,0.52,0.61,0.52,0.58,0.52,0.61,0.52,0.61,0.49,0.61,0.52,0.61,0.49,0.64,0.49,0.58,1.65,0.61,1.65,0.58,1.65,0.58,1.65,0.61,1.62,0.61,1.62,0.58,1.65,0.61,1.62,0.61,1.65,0.58,0.52,0.61,1.65,0.58,0.49,0.64,1.62,0.58,0.52,0.61,0.52,0.61,0.52,0.58,0.52,0.61,1.65,0.58,0.52,0.58,1.65,0.58,0.52,0.61,1.65,0.55,1.68,0.61,1.62,0.61],
    Set100:[8.97,4.42,0.58,0.52,0.61,0.52,0.58,0.52,0.61,0.52,0.61,0.49,0.61,0.52,0.58,0.52,0.64,0.49,0.58,1.65,0.64,1.59,0.61,1.65,0.58,1.65,0.61,1.62,0.61,1.65,0.55,1.65,0.61,1.62,0.61,0.52,0.64,1.59,0.61,1.65,0.58,0.52,0.61,1.62,0.58,0.52,0.64,0.49,0.61,0.52,0.58,1.65,0.61,0.52,0.58,0.52,0.61,1.65,0.58,0.52,0.61,1.62,0.58,1.65,0.61,1.62,0.58],
    Set1:  [9.00,4.39,0.61,0.49,0.61,0.52,0.61,0.52,0.61,0.49,0.64,0.49,0.58,0.52,0.61,0.52,0.61,0.52,0.58,1.65,0.61,1.62,0.61,1.62,0.61,1.65,0.61,1.62,0.58,1.65,0.58,1.65,0.61,1.62,0.61,0.49,0.61,1.65,0.61,1.62,0.58,1.65,0.61,0.52,0.58,0.52,0.58,0.55,0.61,0.49,0.61,1.68,0.58,0.49,0.61,0.49,0.64,0.49,0.61,1.62,0.64,1.62,0.58,1.65,0.58,1.65,0.61],
    Set2:  [8.97,4.42,0.61,0.49,0.64,0.49,0.58,0.52,0.61,0.52,0.61,0.52,0.58,0.52,0.61,0.49,0.64,0.49,0.61,1.62,0.64,1.62,0.61,1.62,0.58,1.65,0.58,1.65,0.61,1.65,0.58,1.65,0.61,1.62,0.58,0.52,0.61,1.62,0.61,1.65,0.58,0.52,0.61,0.49,0.61,0.52,0.61,0.52,0.61,0.49,0.64,1.59,0.64,0.58,0.52,0.52,0.58,1.65,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.58],
    Set3:  [9.00,4.39,0.61,0.49,0.64,0.49,0.58,0.52,0.61,0.52,0.64,0.49,0.58,0.52,0.61,0.52,0.58,0.55,0.58,1.65,0.61,1.62,0.61,1.65,0.58,1.65,0.58,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.61,0.49,0.64,0.49,0.58,1.65,0.64,0.49,0.58,1.65,0.61,0.52,0.58,0.52,0.61,0.52,0.70,1.52,0.61,1.62,0.58,0.52,0.64,1.62,0.58,0.52,0.61,1.65,0.58,1.65,0.61,1.68,0.55],
    Set4:  [9.00,4.42,0.58,0.52,0.61,0.52,0.58,0.52,0.61,0.52,0.61,0.49,0.61,0.52,0.61,0.49,0.64,0.49,0.61,1.62,0.61,1.62,0.61,1.65,0.58,1.65,0.61,1.62,0.58,1.65,0.58,1.65,0.64,1.62,0.58,0.52,0.61,0.52,0.58,0.52,0.58,1.68,0.58,0.52,0.61,0.49,0.61,0.52,0.58,0.52,0.64,1.62,0.58,1.65,0.61,1.62,0.61,0.52,0.58,1.65,0.61,1.62,0.58,1.65,0.64,1.62,0.58]
    };
    

    and after I send it to Puck.js process.memory() output is:

    
    >process.memory()
    ={ "free": 481, "usage": 1519, "total": 2000, "history": 315,
      "stackEndAddress": 536917896, "flash_start": 0, "flash_binary_end": 471892, "flash_code_start": 479232, "flash_length": 524288 }
    
  • Hi - yes, it's an issue with JavaScript's handling of arrays. Since arrays can hold absolutely anything, by default they're not very memory efficient.

    However if you tell Espruino what you want to put in them then you can do much better. Try:

    var cmd={
    On:    new Float32Array([8.97,4.42 ... ]),
    Down:  new Float32Array([8.97,4.42 ...])
    etc
    

    and you should be fine (it'll be around 8 times more efficient!). You can compact them down even more (for instance storing them in an array of bytes), but then you have to worry about scaling.

    There's a bit more information about memory efficiency here: http://www.espruino.com/Performance

  • Thank you! It works! Overall memory usage is 6 times less!

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

Question about Out of memory

Posted by Avatar for SergeP @SergeP

Actions