Why use an object pool?
Because Flash is a managed GC to clean up resources, and only GC knows when to clean up the resources, then our game's performance will be .........
For example, if you click a button in a game, an image is loaded and you click the image again to destroy the image.
If the user does not stop clicking, the user will continuously load and display the destruction. Memory is difficult to control.
From Adobe
Http://help.adobe.com/zh_CN/as3/mobile/WS948100b6829bd5a6-19cd3c2412513c24bce-8000.html
Use the object pool whenever possible.
Another important optimization is called the object pool, which involves repeated use of objects. Before initializing an applicationProgramDuring this period, a certain number of objects are created and stored in a pool,
For example, an array or vector object. After an object is operated, stop the object to avoid it occupying CPU resources, and then delete all references to each other. However,
Do not set referenceNull,This will make it meet the garbage collection conditions. You only need to put this object back in the pool and search for it when you need a new object.
Reusing objects can reduce the need for instantiating objects, and the cost of instantiating objects is high. It can also reduce the chances of running the Garbage Collector, thus improving the speed of application running.
After the object pool is used
We will first create the desired instance and throw it into the objectpool of the Object pool.
- The object pool should be obtained by a singleton.
- Start time: a certain amount of objects are directly assigned to the pool during initialization.
- Use Time: Borrow from objectpool
- Upon destruction (release): return it to objectpool
- Completely released: the pool vector always references the sprite object. To completely delete objects from the memory, use the spritepool classDispose ()To delete all the remaining references.