Chat freely about the open source Javascript projects for ESP8266

User avatar
By hygy
#33050 Hi,

Here is that code, after a while it runs out of memory. I'm doing wrong, or its a bug.

Code: Select allERROR: Out of Memory!
WARNING: Unable to create string as not enough memory
WARNING: Unable to create string as not enough memory
WARNING: Unable to create string as not enough memory
WARNING: Unable to create string as not enough memory
Execution Interrupted



Code: Select allExecution Interrupted
WARNING: Truncating string as not enough memory
WARNING: Out of memory while appending to array
WARNING: Unable to create string as not enough memory
WARNING: Unable to create string as not enough memory
WARNING: Unable to create string as not enough memory
WARNING: Unable to create string as not enough memory
Execution Interrupted during event processing.
at line 65 col 5
    );
    ^
in function "sendTs" called from line 72 col 24
  sendTs(field1, field2);
                       ^
in function called from system



Here is the source code:

Code: Select allvar ssid = "ssid";
var pass = "pass";
var ApiKey = 'apikey';

var peri = 30; //period of reading sensor in sec.

var iotHostName="thingspeak.com";
var iotIpAddr="";

var connectToWifi = function() {
  console.log('connecting to wifi');
  var GPIO2 = new Pin(2);

  digitalWrite(GPIO2, 1);
  ESP8266WiFi.init();
  ESP8266WiFi.connect(ssid, pass, function() {
    digitalWrite(GPIO2, 0);
    console.log('connected to wifi');
    var ipInfo = ESP8266WiFi.getIPInfo();
    print("Current IP address is: " +
      ESP8266WiFi.getAddressAsString(ipInfo.ip) + ':' +
      ESP8266WiFi.getAddressAsString(ipInfo.netmask));

    resolveIotAddress(repeat);

  });
};

var resolveIotAddress= function(callback)
{
  var ESP8266 = require("ESP8266");
  ESP8266.getHostByName(iotHostName, function(address) {

    iotIpAddr = ESP8266.getAddressAsString(address);
    console.log('resolved:' + iotIpAddr);

    callback();

  });

};

var sendTs = function(press, temp) {


    var http = require("http");
    http.get({
        host: iotIpAddr,
        port: 80,
        headers: {
          'Host': 'tingspeak.com',
        },
        path: '/update?key=' + ApiKey + '&field1=' + press + '&field2=' + temp
      },
      function(response) {
        print("get callback!");
        response.on('data', function(data) {
          print(data);
        });
        response.on('close', function() {
          print("The response connection closed");
        });
      }
    );

};

repeat = function() {
  var field1 = 1 + Math.floor(Math.random() * 40);
  var field2 = 1 + Math.floor(Math.random() * 40);
  sendTs(field1, field2);
  setTimeout(repeat, peri * 1000);
};


E.on('init', function() {
  connectToWifi();
});


thanks
HyGy
User avatar
By kolban
#33073 One of the biggest problems we have in the JavaScript implementation is the concept of "memory leaks". What this means is that we allocate some memory within the internal implementation and fail to release it. If the same code path is followed over and over again, we eventually run out of memory.

One of the ways to detect this is to code:

print(process.memory());

and watch the amount of free memory. If it declines over time, we have a leak. The good news is that when we figure out what is leaking, we can close it up very quickly. The bad news is that it can be hard to figure out what calls are leaking memory.

My advice is to "sprinkle" the print statements shown above throughout your code and look for places where memory is being allocated and not released. Once you find one or more of these, you can contact us through the forum or gitter stream chat and we'll jump right on it and get the problem taken care off.
User avatar
By hygy
#33126 I put process.memory() many places, but I cannot se any leaking.

The smallest value is this:
{ "free": 183, "usage": 840, "total": 1023, "history": 45 }

the highest is:
{ "free": 455, "usage": 568, "total": 1023, "history": 45 }