Out of memory while appending to array!!!!

Posted on
  • Hi I am trying to use NRF module... I am inserted NRF module as a variable in "LIB/jswrap_***.c" file
    and i am getting that variable data and using "eval" to use that module... up to some time it is working good...
    but after some time i am getting "Out of memory while appending to array"
    what exactly this means? what things we need consider to get out of this problem.....

    Here i am attaching the file which i am using in firmware...


    1 Attachment

  • Interesting! Why do you do that instead of just loading it with the Web IDE?

    Out of memory while appending to array means that Espruino was trying to add something onto the end of an array, but couldn't because there wasn't enough memory to allocate a variable for the array's index.

    It may not be related to what you're doing with the NRF module at all - it might just be that your code is creating an array with too many elements.

    For example the following code may well fail with that error eventually:

    var arr = [];
    setInterval(function() {
      arr.push(Math.random());
    }, 100);
    

    Maybe keep an eye on the value of process.memory().usage and you'll get an idea of what is causing the memory usage to rise.

  • Hi Gordon,
    I am trying to update my application over the air... to do that i am reducing my application size by keeping module in the main firmware.....
    coming to Out of memory..... if i am using" Nrf.Sendstring();" i am facing the problem...when i am calling this i am checking "process.memory().usage" it is continuously decrementing....
    how to control this.....

  • Are you using an up to date firmware? Some older firmwares might have had some memory leaks that could have caused problems.

    Otherwise, please could you run trace() and copy the output to a file, then run nrf.sendstring(..) a few times, and run trace() again and save the output. It'll contain your code, so if you're worried about sharing it online you could e-mail it to me.

    It should help me to find out why the memory usage is increasing.

  • Hi Gordon,
    in this project we are trying transmit GPS data on "NRF"... this is the script which i am using.

    var data=command.test();
    var arr = [];
    data+=command.gps();
    
    eval(data);
    eval(Modvar.nrf());
    Serial2.setup(9600);
    Serial5.setup(9600);
    Serial5.on('data', function (data) { print(+data); });
    function onInit(){nrf.init([0,0,0,0,2],[0,0,0,0,1­]);}
    onInit();
    setInterval(function() {
      nrf.masterHandler();
    //  print(process.memory());
    }, 50);
    
    function nrf_print(data){
    
     // var i =nrf.sendString("\n"+data);
      print("result :"+data);
       
    }
    
    
    //eval(command.gps());
    
    var gps =ConnectGps(Serial2, function(data) {
    
       if(data.tag == "GGA")
       {
         console.log("\r\n lat:" +data.lat);
         console.log("\r\n lon:" +data.lon);
         console.log("\r\n fix:" +data.fix); 
    
         // lat
         arr[6] =  parseInt(data.lat[0],10)*10 +  parseInt(data.lat[1],10);
         arr[7] =  parseInt(data.lat[3],10)*10 +  parseInt(data.lat[4],10);
         arr[8] =  parseInt(data.lat[5],10)*10 +  parseInt(data.lat[6],10);
    
         // lon
         arr[9] =  parseInt(data.lon[0],10)*10 +  parseInt(data.lon[1],10);
         arr[10] =  parseInt(data.lon[3],10)*10 +  parseInt(data.lon[4],10);
         arr[11] =  parseInt(data.lon[5],10)*10 +  parseInt(data.lon[6],10);
       }
    
    
     else if(data.tag == "RMC")
      {
    
        console.log("\r\n time:" +data.time);
        console.log("\r\n date:" +data.date);
        console.log("\r\n speed:" +data.speed);
    	console.log("\r\n valid:" +data.valid);
    	console.log("\r\n course:" +data.course);
    
        // time  
        arr[0] =  parseInt(data.time[0],10)*10 +  parseInt(data.time[1],10);
        arr[1] =  parseInt(data.time[3],10)*10 +  parseInt(data.time[4],10);
        arr[2] =  parseInt(data.time[7],10)*10 +  parseInt(data.time[8],10);
    
        // date
        arr[3] =  parseInt(data.date[0],10)*10 +  parseInt(data.date[1],10);
        arr[4] =  parseInt(data.date[3],10)*10 +  parseInt(data.date[4],10);
        arr[5] =  parseInt(data.date[7],10)*10 +  parseInt(data.date[8],10);
    
        // valid
        arr[12] = data.valid[0];
    
        // speed 
        arr[13] = parseInt(data.speed,10);
        nrf_print(arr);
      }
       
    });
    

    in this

    nrf_print(arr);
    

    if i changed this one to static data like nrf_print("Helloworld"); there is no problem with memory... i think this "arr "array is creating some problem...

    Thanks
    Vishnu

  • Are you using the newest firmware? There was a bug that could have caused a memory leak during early this kind of thing. As I said before, the traces would really help.

  • Hi Gordon, i am using latest firmware.....
    what was the bug...?

    i will try using "trace"....

  • Hi Gordon,

    I have used trace() but ... unable to understand, here i am attaching the files....
    please have a look at this....

    and after running " nrf_print("helloworld");" this one in console ... i am getting error as

    in function called from system
    Uncaught Error: Field or method "cjarCodeAt" does not already exist, and can't create it on String
    at line 4 col 16
    c1 = sentence.cjarCodeAt(i);

                ^
    

    in function "Checksum" called from line 6 col 28

    if ( Checksum(c[0],c[1]) ) {
                            ^
    

    in function "handleGPSLine" called from line 7 col 35

      handleGPSLine(line, callback);
                                   ^
    

    in function called from system
    at line 4 col 16
    c1 = sentence.cjarCodeAt(i);

                ^
    

    in function "Checksum" called from line 6 col 28

    if ( Checksum(c[0],c[1]) ) {
                            ^
    

    in function "handleGPSLine" called from line 7 col 35

      handleGPSLine(line, callback);
                                   ^
    

    in function called from system


    1 Attachment

  • Hmm. Well, charCodeAt changing to cjarCodeAt looks like a memory corruption error, and it might explain why memory is getting lost.

    To be honest the trace is only useful for bigger programs when you run a graphical diff to compare 'before' and 'after'.

    Which board were you running this on? If possible, can you make clean and compile a version of the firmware without RELEASE=1, and if possible with DEBUG=1 on the command-line. You might have to remove USE_FILESYSTEM, USE_GRAPHICS and USE_NET from the Makefile to make room for the debug info, but it looks like you don't use them anyway.

    It should compile in some assertions that may catch what the problem is. It's odd though, because I haven't noticed an error like that in 6 months or more.

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

Out of memory while appending to array!!!!

Posted by Avatar for Vishnu @Vishnu

Actions