Homeyscript memoryleaks

Homeyscript seems to have a memoryleak. Memory usage of homeyscript is growing very quickly after restarting the app or rebooting homey.
Memory usage grows very quickly to about 30MB and then it takes a few hour to grow furher to 75MB. Thats the point where Homey disables the app.

I have about 30 scripts. The scripts are simple. Most do only a fetch to a server. Even when there are no scripts executed memory usage is growing.

Are more people seeing this? Or maybe solved this? I am thinking about re-installing Homeyscript. But does that mean that i loose all scripts?

I have about 20 scripts, which don’t get executed (mostly test-scripts) and I don’t see any memory leaks. My first guess would be that the memory leak occurs inside your scripts, or perhaps within one of API’s that Athom exposes. I will try some tests to see if perhaps fetch (which I assume you use) leaks memory.

It does. After making a few 100 requests without consuming the response body (see below), HomeyScript got paused because it was using too much memory:

This doesn’t seem to leak memory:

const response = await fetch(URL);
const data = await response.text();

However, if you don’t consume the body (i.e. don’t use response.{text,json,blob}) it will leak memory. So always consume the body, even if you don’t actually use it.

1 Like

@robertklep Thank you very much. I also suspect fetch.

Only i use fetch for sending commands (In JSON) to the nodejs server. This server is used to control my sonos system and samsung tv

It doesn’t really matter if you’re sending commands or receiving data, to prevent the leak (at least the one in HomeyScript’s fetch) you have to consume the response body.

I have modified all scripts where i use fetch. I let you know if it worked. But for now many thanks

Hello @robertklep. I have changed all scripts but don’t see any difference compared to the old situation. I have also written a small script to test but i don’t think that fetch is my problem

function f(counter) {
fetch('http://192.168.1.222:3002/test',{
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        test: 'abcd' 
    })
}).then(async response => {
        await response.text();
        const apps = await Homey.apps.getApps( );
        const app = apps['com.athom.homeyscript'];
        total = ((app.usage.mem) / (1000*1000));
        console.log('app.usage ' + counter + ": ", total)
    }).catch(e => log(e));

}
const wait = (timeout) => (new Promise( resolve => _.delay( _ => resolve(), timeout)))

async function execute() {
let counter = 0;
let total = 0
while(++counter < 200) {
    f(counter);
    await wait(1000)
}    

}
execute()

During the test mem usage is around 40MB and does not change much. But over time (a few hours) mem usage is growing. Even at night when there are hardly any cals done to the service

In this test script i do a await response.text(); I see no difference between with and without this line of code

Have you tried commenting out the Homey.apps.getApps() part of your test to rule out that that’s the cause of the leak?

I’m afraid that when you keep seeing memory leaks, all you can do is turn off scripts (or rather, flows that execute scripts) one at a time to see if that fixes it, so you can track down if there is a particular script at fault.

Homey.apps.getApps() is only here for this test. I don’t use that in my regular scripts.

I’am afraid you are right, i have to go through all my scripts and disable one by one. One thing i could try before that is removing Homeyscript and reinstall it. But im not sure if i will loose all my scripts then. Do you have experience with that?

I experience the same problem. The homescript app pauses after a few days. I need to restart the app and it runs again (for a few days). Most of the scripts receives MQTT data every 5 seconds from my energy automation system, 10 variabeles in total. I switched from the logic variables to the better logic app but it does not solve the problem. Is it possible or necessary to unload local variables?

Edit:
I found a tip on a website about memory leaks and javascript.
Add as first row of the script: ‘use strict’;
Not yet sure of it is the solution, it take a couple of days before the app pauses again.

Hello,
I’m gettin the same issue. I get temperature (1 sensor) and fan speed (4 fans) from MQTT every 5 seconds. I set a tags with that values and update virtual sensors values the tag values.
Very simple scripts:

var MyNum1 = parseInt(args[0]);
await setTagValue(“RetSpeed1”, {type: “number”, title: “RetSpeed1”}, MyNum1);
return true;

Every few days I get the app paused for over memory usage and have to restart it manually.
Any ideas?
Thank you
Regards
Marcello

@Hunters Did you try the MQTT Device from the MQTT Hub app?
It’s build exactly for your case: mapping mqtt topics to (virtual) device capabilities.

Hello,
not yes tried it… Will give it a look in next days.
Thank you for suggestion.
Bye

Marcello
[mod break: removed private data]

I restart homeyscript every night. Since that moment never had any issues again

Hello,
for sure that will be my last step if I don’t find any other solution.

moderator removed private info.

Yeah, not the nicest solution. But at this moment, unfortanetely the best way. The hue app has the same problems

So this issue still hasn’t been resolved (in version 7.2.0 of Homey and version 3.2.6 of Homeyscript), which I find quite unacceptable. Homeyscript is Athom’s own app, and it should not leak memory by itself.

It doesn’t in my Homey (2016), maybe it’s in your code?
Did you already try to disable your routines one by one?

If my scripts were leaking, the memory consumption should be on a steady rise (scripts are called quite freqently, with just a few seconds between each call). It is not:

As you can see, it stays constant for almost two days, then suddenly eats a fairly large chunk of memory again.

And my scripts are extremely simple. Here’s one:

// Converts received MQTT value in argument to a tag called ams_voltage

var number = parseFloat(args[0])

await tag("ams_voltage", number);

return true;

How can that leak any memory? It is just parsing an argument and writing it to a tag. I am not well versed in javascript, but I can’t see how that could leak memory.

How often is your script called?