Nodejs genetic algorithm memory management?

77 Views Asked by At

I'm trying to implement a genetic algorithm and have the problem of heap memory getting too large. To the extend that it throws an error. I'm afraid it has something to do with my population array. I displayed the memory usage while running the program and recognized it is growing and growing and never gets freed up.

Consider the following setup:

let used = {}

class Trainer {
    static breed = function(elite) {
        //code for making a new generation
        return brood
    }
    constructor() {
        this.population = []
    }
    async evolve(data, options, callback) {
        // potential point where the error may be
        // because I see the memory growing and never getting smaller
        // but I think it should get smaller because I don't
        // reference the old generation any more or do I ?
        while (error >= threshold) {
            // train each member of the population async
            // and than make a new population out of the elite
            const elite = this.population.slice(0, 10)
            this.population = Trainer.breed(elite) <--- ¯\_(ツ)_/¯
            callback(info)
            used = process.memoryUsage()
        }
    }
}

const trainer = new Trainer
const data = getData()
const options = makeOptions()

function log(info) {
    console.log(info)
    console.log(used)
}

async function test() {
    await trainer.evolve(data, options, log)
}

test()

Now I have some questions.

Is it true that on each generation the old array is not referenced anymore and get's garbage collected and makes some free space in memory?

How can I analyze the memory usage by each function to get better inside of where the problem may is in my code?

Because the code is very large I tried to simplified my problem. But if you are interested in the complete code please have a look in my repo: https://github.com/kiro7shiro/image-generator You can find the described setup from above in the "/cli/image-generator-evolve.js" file.

0

There are 0 best solutions below