[SOLVED] [TS] cc.instantiate() and destroy() memory leak

I would love some help in this one. I cannot figure out the memory leak issue.

I have around 10 prefabs and the only thing I want to do is to replace one Prefab with another. Destroy the node (not the prefab) that the prefab is in, to release memory.

const newPrefabNode = instantiate(prefab);
this.node.addChild(newPrefabNode);
...
this.node.child[0].destroy(); /* or this.node.destroyAllChildren(); doesn't matter in this case */
const anotherNewPrefabNode = instantiate(anotherPrefab);
this.node.addChild(anotherNewPrefabNode);

I also tried:
somewhere in the code:

this.node.destroyAllChildren(); /* or this.node.children[0].destroy(); */

Then having in the update method:

update(dt) {
  if (!cc.isValid(this.node.children[0])) {
    const prefabNode = cc.instantiate(prefab);
    this.node.addChild(prefabNode);
  }

Cause I read that:

Destroy this Object, and release all its own references to other objects. Actual object destruction will delayed until before rendering. After destroy, this CCObject is not usable any more. You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
http://www.cocos2d-x.org/docs/creator-api/en/classes/Node.html#destroy

I instantiate a lot and in a loop. Wonder why the delete()doesn’t release any memory since the memory goes up fast and after a while it starts lagging because of memory real issues. delete() doesn’t seem to do anything for me.

Please help <3

Found stuff like this now:
http://www.cocos2d-x.org/docs/creator/en/scripting/pooling.html?h=pool
http://www.cocos2d-x.org/docs/creator-api/en/classes/NodePool.html?h=pool

Seems quite relevant. If you have comments regarding it, please leave it here. Otherwise I will be back with a solution if and when I have one.

Thank you!

Do you have any addEventListerners that are not removed?

As the links I sent in the last post says:

Creating and destroying node and component instance (with cc.instantiate and node.destroy during runtime is very inefficient and can cause frame rate to drop if there’re too many of those going on.

Using NodePools solved my issue.

I cannot believe no one could help me with this, seems like a really common use-case! :hushed:

Since we don’t know what code is attached to your prefab it’s a little hard to help.