• Some pointers about the implementation of JS in Espruino is very welcome here. I provide an example from code I'm working on (Part of this conversation - including some helpful responses - began in http://forum.espruino.com/conversations/255757/ about GPS module and Extensions there of. It is now its own conversation for separation of concerns.)

    I needed an array of strings - keys - for processing. So I created an array of strings in the source code and it got me quickly out of memory ( #outofmemory ) into the 'out of memory jail':

    var keys = 
    [ "key000"
    , "key001"
    , "key002"
    , "key003"
    // ...and many more...
    , "keyNM9"
    ];
    keys.forEach(function(k) { console.log(k); }
    

    In a desperate move - with no real rational but just some guessing - I chose to group the keys w/ space as separator in fewer strings and apply some joining and splitting before processing to keep the same algorithm (because of performance) - and 'miraculously - it got me out of 'out of memory jail' (The joining-splitting with/by space is possible because the keys cannot include spaces):

    var keys = 
    [ "key000 key001 key002 key003 key004 key005 key006 key007 key008 key009"
    , "key010 key011 key012 key013 key014 key015 key016 key017 key018 key019"
    , "key020 key021 key022 key023 key024 key025 key026 key027 key028 key029"
    // ...and many more...
    , "keyNM0 keyNM1 keyNM2, keyNM3, keyNM4, keyNM5, keyNM6, keyNM7, keyNM8, keyNM9"
    ];
    keys = keys.join(" ").split(" ");
    keys.forEach(function(k) { console.log(k); }
    

    There are several conversations out there about memory and out of memory and I reference them in this list:

    1. http://forum.espruino.com/conversations/1724/ clearTimeout and WARNING: Out of memory while appending to array
    2. http://forum.espruino.com/conversations/1578/
      GPS code/module/library leaking memory before 'satellite lock' occurs
    3. http://forum.espruino.com/conversations/1192
      Out of memory?
    4. [http://forum.espruino.com/conversations/1032](http://forum.espruino.com/conversations/1032/ Memory usage

    But none of them can explain the following:

    You may have notice in the above code the line keys = keys.join(" ").split(" ");

    Even though I then kept/stored the joined-splitted-processed array, I was kept out of 'out of memory jail'. It must have to do something with how source code of arrays of string literals is pulled in and hold onto.

    What is the 'somewhat puzzling' key-note to take from this experience?

    PS: I had temporarily similar experience as mentioned in 2nd reference above, even though I used the fixed version of the .indexOf(...) - [1v70]. The issue showed in the GPS modules handleGPSLine when going for the decimal points with indexOf('.') in the lines received from the GPS receiver. Could it be that the .indexOf() String method is still a bit 'weak on its legs'. Having read about it I felt it had something to do by releasing the search argument string... but I may be totally wrong about ti. As said: it was temporary. Nevertheless, observing it cannot hurt.

About

Avatar for allObjects @allObjects started