How to clean cache like THREE.Cache.clear();

Is there any cache clean function in playcanvas?

My script just clone the avatar and destroy when I click the button.
But It seems to be there are some memory leak, so I want to refresh the memory when destroy clones.

This is my code.

var CloneTest = pc.createScript('cloneTest');
var d;

// Initialize code called once per entity
CloneTest.prototype.initialize = function() {

    // Register the mouse down and touch start event so we know when the user has clicked
    start_as = pc.app.root.findByName("Button_as");
    pause_ax = pc.app.root.findByName("Button_ax");
    stop_ax = pc.app.root.findByName("Button_ad");
    
    f=pc.app.root.children;
    count_a = pc.app.root.findByTag("avatarcount")[0].element;

    console.log(count_a.text);

    start_as.button.on('click', function(event){
        if(event.button == pc.MOUSEBUTTON_LEFT){
            d=true;
            setInterval(function(){
                this.TestClone(event);
                    if(d==true){
                        console.log("clone start! " + "Avatar:  " + f.length);
                        count_a.text=f.length;
                        console.log(f.length);
                        console.log(d);
                        }
            }.bind(this),1000);
        }
    },this);

    pause_ax.button.on('click',function(event){
        if(event.button == pc.MOUSEBUTTON_LEFT){
            d=false;
        }
    }, this);

    pause_ax.button.on('touchstart',function(event){
        d=false;
    }, this);

    stop_ax.button.on('click',function(event){
        if(event.button == pc.MOUSEBUTTON_LEFT){
            this.DeleteClone(event);}
            count_a.text=f.length;
    }, this);

    stop_ax.button.on('touchstart',function(event){
        this.DeleteClone(event);
        count_a.text=f.length;
    }, this);

};


CloneTest.prototype.TestClone = function(event) {

    if(d==true){
        cloned = pc.app.root.findByName("POC_dance");
        e = cloned.clone(); // Clone Entity
        e.setPosition(0,0,0);
        this.app.root.addChild(e);
    } else if (d == false){
    }
};


CloneTest.prototype.DeleteClone = function(event) {

    for (var k = f.length - 1; k >= 1; k--) {
        r=pc.app.root.children[k];
        r.destroy(); // destroy Entity
    }
};

Do you have a project example where the leak can be seen? In the code posted above, I can’t see where memory would be allocated to have the memory leak.

https://playcanvas.com/project/1053173/overview/blank-project

This is my project :slight_smile:

Can you explain the steps to produce the leak and also how you are seeing that there is one please?

My project is working as below.

  1. Start : clone the avatar.
  2. Pause : stop the cloning.
  3. Stop : destroy the avatar.

I can see the memory leak throw the Performance monitor of chrome.
If I click the stop button to destroy the avatar, I can see the CPU usage is initialized, but the JS heap size is not initialized.

How do you do these steps in the app, all I have are:

Can you give me a list of very specific steps of which buttons to press and in which order please?

It works fine for me.

I use memory tab in chrome to monitor the memory. I am around 20-25 Mb when applicaiton started. I click start and the app starts to make clones. The memory grows to 100+ Mb when I have some 100 clones. I press pause and stop. Clones are cleared and after a few seconds the memory returns back to 20-25 Mb.

I noticed only the first “cycle” is ok. If you start cloning again, without refreshing it, it will clone double, almost immediately. On the next cycle, it will clone tripple, or 3 times at a time. There is an issue with your code, probably a timer not cleared or something. Doesn’t affect the memory though, as it is cleared for me after pause and stop.

1 Like