Memory Leak when too deep in the scope
pietro-lopes opened this issue ยท 3 comments
When reloading it gets obvious, every reload it leaks the KubeRecipeEvent
Current workaround is trying to not use as much lambda as possible so it does not trigger parent scope stuff
ServerEvents.recipes(event => {
let cobbleGen = [
'cobblestone',
'andesite',
'calcite',
'cobbled_deepslate',
'diorite',
'dripstone_block',
'granite'
]
// THIS ONE LEAKS
// cobbleGen.forEach(item => {
// event.recipes.minecraft.crafting_shapeless(`2x minecraft:${item}`, ["lava_bucket","water_bucket", `minecraft:${item}`])
// .id(`kubejs:poormans_cobble_gen/${item}`)
// })
// THIS ONE DOES NOT
for (let item of cobbleGen) {
event.recipes.minecraft.crafting_shapeless(`2x minecraft:${item}`, ["lava_bucket","water_bucket", `minecraft:${item}`])
.id(`kubejs:poormans_cobble_gen/${item}`)
}
})
Update on that, I found out that when a Java Class is first called, it is cached, with the scope of this call.
This script proves this concept and does not leaks because the first scope that was cached on the call of ShapedRecipe class is not a inner scope.
const cobbleGen = [
'cobblestone',
'andesite',
'calcite',
'cobbled_deepslate',
'diorite',
'dripstone_block',
'granite'
]
ServerEvents.recipes(event => {
// THIS ONE DOES NOT
for (let item of cobbleGen) {
event.recipes.minecraft.crafting_shapeless(`2x minecraft:${item}`, ["lava_bucket", "water_bucket", `minecraft:${item}`])
.id(`kubejs:poormans_cobble_gen/${item}`)
}
})
ServerEvents.recipes(event => {
// THIS ONE LEAKS BUT NOW IT DOES NOT :)
cobbleGen.forEach(item => {
event.recipes.minecraft.crafting_shapeless(`2x minecraft:${item}`, ["lava_bucket", "water_bucket", `minecraft:${item}`])
.id(`kubejs:leaking_poormans_cobble_gen/${item}`)
})
})
As I tested, passing the top most scope is good enough for caching.
can be now
members = new JavaMembers(cl, includeProtected, cx, ScriptableObject.getTopLevelScope(scope));