Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a simple Node.js application that is used to communicate with a Scratch project. It works fine but its memory consumption keeps increasing, therefore, I think that there is a memory leak somewhere in the program.

Ultimately, I need help with identifying and fixing this memory leak. Thanks!

Here is the Node.js code:
node.js
var Scratch = require('scratch-api');
const fetch = require('node-fetch');
Scratch.UserSession.create("username", "password", function (err, user) {
    user.cloudSession(419697811, function (err, cloud) {
        let code = ["", "", "", "", "", "", "", "", "", "", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", ".", " ", "_"]
        function decode(encoded) {
            var letter_num = 0
            var value = ""
            var idx;
            do {
                idx = encoded.charAt(letter_num) + encoded.charAt(letter_num + 1)
                letter_num = letter_num + 2
                if (Number(idx) < 1) { //evaluates it as a number rather than a string
                    break
                }
                value = value + code[Number(idx)]
            } while (Number(idx) >= 1)
            return value
        }
        function encode(val) {
            var temp = "" //so it concatenates strings rather than adding numbers
            var letter_num = 1
            for (letter_num = 0; letter_num < val.length; letter_num++) {
                temp += code.indexOf(val.charAt(letter_num))
            }
            temp += "00" //otherwise it would evaluate to 0
            return temp
        }

        var idle_counter = 0;

        async function getCount(username) {
            var res = await fetch(`https://api.scratch.mit.edu/users/${username}/messages/count`);
            var data = await res.json()
            if (!data.code) {
                return data.count;
            }
            throw new Error(`ERROR - The user ${username} could not be found!`)
        }

        async function updateStates() {
            var cloud_check = cloud.get('☁ checkMessage')
            if (cloud_check > 0) {
                if (cloud_check == 1) {
                    cloud.set('☁ checkMessage', 2)
                } else if (cloud_check == 2) {
                    idle_counter++
                    if (idle_counter > 5) {
                        cloud.set('☁ checkMessage', 0)
                        idle_counter = 0
                    }
                } else if (cloud_check == 3) {
                    var username = decode(cloud.get('☁ message'))
                    var message;
                    try {
                        var count = await getCount(username)
                        message = encode(`success.${count}`)
                        //console.log(`SUCCESS - Successfully retrieved the message count (${count}) for the user ${username} !`)
                    } catch (e) {
                        message = encode("error.user not found")
                        //console.log(e.message)
                    }
                    cloud.set('☁ message', message)
                    cloud.set('☁ checkMessage', 4)
                    idle_counter = 0
                } else {
                    idle_counter++
                    if (idle_counter > 5) {
                        cloud.set('☁ checkMessage', 0)
                        idle_counter = 0
                    }
                }
            }
        }

        async function timeout() {
            setTimeout(async () => {
                await updateStates();
                timeout();
            }, 2000);
        }

        //console.log('%cINITIAL', 'color: green;');
        timeout() //a loop
    });
});


What I have tried:

I had the same problem with a previous version of this application and, following this answer on Stack Overflow, I changed the code slightly to fix that memory leak. However, it looks like that fix did not work.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900