CreateJS / EaselJS

The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.
http://createjs.com/
MIT License
8.11k stars 1.97k forks source link

How to see number of cached items? #1045

Closed EyesightTechnology closed 3 years ago

EyesightTechnology commented 4 years ago

Hi, just a few questions here.

First and foremost, how do you see the number of cached items? I want to make sure every item I cache is at some point destroyed, to avoid memory leaks.

  1. Just to clarify, will having too many cached items cause your program to run slower? I assume so. And it will cause a memory leak, right?

  2. This is a more general question, but the way I'm handling caching is by creating my own Text and Rect classes. (which are used with my own custom code for abstracting, not just for the sake of existing) In the constructor, these classes cache an item. Then a destroy method is used to uncache an item. Is that a good practice?

danzen commented 3 years ago

Hi @EyesightTechnology - you are welcome to ask questions at StackOverflow for perhaps a quicker response. Anyway, from my experience, you do not need to cache everything. Caching text will not quite look as good and you probably only need to if you have a lot of text or really are trying to get the most out of performance. So you may not want to make caching your default.

You can also separate the text onto another stage so it does not have to update. Then put the parts that update a lot on a different stage.

In terms of the number of cached objects... I don't know of a way to find that out. You could keep track of them somewhere. But your destroy method sounds good - it would be up to the developer to then destroy them. I use the word dispose - it is a little friendlier sounding - but up to you.

I have always assumed that removing an object from the stage and removing all references to the object will garbage collect the object and its cacheCanvas if it has one. I could be wrong so if anyone knows differently, let us know. To be safe you can clear the cache in your destroy as you say.

I don't think having too many cached objects is an issue and it should run faster if those cached objects are vector based and you are not updating cache a lot. But like I said, for the most part, I do not cache. Only if I start noticing performance issues or suspect there may be performance issues on slower devices because I find it takes a little away from the quality. Caching before animating is sometimes a good idea as the movement hides the quality issues.

Hope any of this helps. If you want to discuss more, I am part of a facebook and slack group - let me know.