PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 904 forks source link

Chunks don't unload, memory leak #1123

Closed algj closed 4 years ago

algj commented 4 years ago

Versions

Detailed description of a problem

Memory leak when traveling, chunks don't unload. Node.js will crash if you load too many chunks.

Current code

let mineflayer = require('mineflayer');

//heap grows when traveling
setInterval(()=>{
    let v = process.memoryUsage();
    console.log(v.rss/1000000+"MB", v.heapTotal/1000000+"MB");
},1000);

var bot = mineflayer.createBot({
    host: "127.0.0.1",
    port: 25565,
    username: 'test_bot',
    version: "1.12.2"
});

bot.on('message', function(message) {
  bot.chat(message);
});
bot.on('login', i=>console.log('Joined.'))
bot.on('end', i=>{
    console.log('Left.')
    process.exit();
})
bot.on('error', err => console.log(err))

Expected behavior

Unload chunks when bot is out of rendering distance.

Additional context

I manually teleported bot around the world just to make sure that it wasn't some bug in my code. I noticed that heap memory doesn't go lower.

algj commented 4 years ago
bot.on('chunkColumnUnload',(point)=>{
    console.log(point);
})
// output:
// Vec3 { x: NaN, y: 0, z: NaN }

This is weird. delColumn gets undefined chunkX, chunkY.

I'll do a PR.

Karang commented 4 years ago

In 1.12, the x/z values are names chunkX, chunkZ: https://github.com/PrismarineJS/minecraft-data/blob/master/data/pc/1.12.2/protocol.json#L1478 It needs to be fixed here https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/blocks.js#L262 But first, check if it was always missnamed, or if it change from one version to an other.

Edit: I checked, it seems the unload_chunk packet was introduced in 1.9 and already used chunkX/chunkZ 1.8 doesnt seems to have an unload packet.

algj commented 4 years ago

Made a PR. I wonder how this problem was unnoticed for so long?

rom1504 commented 4 years ago

I guess people don't move their bot around that much. If you don't look for it it'll get noticable only if you load like 10x the view size. You'd have to do something a bit special (auto exploration I guess) to get to this point

Thanks for finding and fixing this bug !

On Fri, Jul 10, 2020, 16:53 Alg notifications@github.com wrote:

Made a PR. I wonder how this problem was unnoticed for so long?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/mineflayer/issues/1123#issuecomment-656717632, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR437V2CT2M7T7CLHSFOQLR24TO5ANCNFSM4OV5TXQA .

IdanHo commented 4 years ago

must have missed it when i wrote that, my bad :disappointed_relieved:

rom1504 commented 4 years ago

thanks