Homey Community Forum

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?