第九章 Libgdx内存管理
Android游戲開發(fā)群:290051794
Libgdx游戲開發(fā)框架交流群:261954621
?
游戲是使用資源較多的應(yīng)用。圖像和音效會(huì)占用大量的內(nèi)存。此外,這些資源大部分不是通過(guò)java的垃圾回收器管理,而是由本地驅(qū)動(dòng)管理。使用垃圾回收器對(duì)紋理等進(jìn)行回收不是一個(gè)明智的做法。
我們需要合理的控制資源的生命周期。在Libgdx中有多個(gè)類實(shí)現(xiàn)這個(gè)功能。它們都實(shí)現(xiàn)Disposable接口指明在生命周期結(jié)束后要銷毀的類的實(shí)例。錯(cuò)誤的釋放資源會(huì)導(dǎo)致內(nèi)存泄露。
下面的類需要手工處理:
AssetManager
Bitmap
BitmapFont
BitmapFontCache
CameraGroupStrategy
DecalBatch
ETC1Data
FrameBuffer
Mesh
ParticleEffect
Pixmap
PixmapPacker
ShaderProgram
Shape
Skin
SpriteBatch
SpriteCache
Stage
Texture
TextureAtlas
TileAtlas
TileMapRenderer
com.badlogic.gdx.physics.box2d.World
當(dāng)資源不再使用應(yīng)盡快的進(jìn)行釋放,釋放與它們有關(guān)的內(nèi)存。訪問(wèn)一個(gè)已經(jīng)釋放的資源會(huì)導(dǎo)致未定義錯(cuò)誤,所以要處理所有資源的引用。
當(dāng)你想知道一個(gè)類是不是需要釋放,檢查它是否有dispose()方法。如果有,那么你正在使用本地資源。
對(duì)象池
對(duì)象池是重新使用閑置的或“死掉”的對(duì)象,而不是每次都創(chuàng)建新的對(duì)象。這通過(guò)一個(gè)對(duì)象池實(shí)現(xiàn),當(dāng)你需要一個(gè)對(duì)象時(shí),從對(duì)象池中獲得。如果對(duì)象池有可用的對(duì)象,則返回。如果對(duì)象池是空的,或者沒(méi)有閑置的對(duì)象,會(huì)創(chuàng)建一個(gè)對(duì)象的一個(gè)新的實(shí)例并返回。當(dāng)你不再需要一個(gè)對(duì)象,并釋放它。就意味著它返回到對(duì)象池。通過(guò)這種方式,對(duì)象分配的內(nèi)存重新被使用。
對(duì)于需要生成大量對(duì)象,比如子彈,障礙,怪物等等,的游戲來(lái)說(shuō),這對(duì)內(nèi)存管理是重要的。
Libgdx對(duì)簡(jiǎn)單池提供了一些工具:
Poolable interface
Pool
Pools
實(shí)現(xiàn)Poolable接口意味著你需要在對(duì)象中實(shí)現(xiàn)reset()方法,它會(huì)自動(dòng)的釋放你的對(duì)象。
下面是一個(gè)簡(jiǎn)單的例子:
public class Bullet implements Poolable {public Vector2 position;public boolean alive;/*** Bullet 構(gòu)造函數(shù),初始化變量。.*/public Bullet() {this.position = new Vector2();this.alive = false;}/*** 初始化Bullet當(dāng)從對(duì)象池中獲取Bullet是調(diào)用。*/public void init(float posX, float posY) {position.set(posX, posY);alive = true;}/***對(duì)象空閑的回調(diào)方法,自動(dòng)被 Pool.free()調(diào)用。*/public void reset() {position.set(0,0);alive = false;}/*** 更新*/public void update (float delta) {// update bullet positionposition.add(1*delta*60, 1*delta*60);// if bullet is out of screen, set it to deadif (isOutOfScreen()) alive = false;}} 在游戲中:public class World() {// array containing the active bullets.private final Array<Bullet> activeBullets = new Array<Bullet>();// bullet pool.private final Pool<Bullet> bulletPool = new Pool<Bullet>() {@Overrideprotected Bullet newObject() {return new Bullet();}};public void update(float delta) {// if you want to spawn a new bullet:Bullet item = bulletPool.obtain();item.init(2, 2);activeBullets.add(item);// if you want to free dead bullets, returning them to the pool:Bullet item;int len = activeBullets.size;for (int i = len; --i >= 0;) {item = activeBullets.get(i);if (item.alive == false) {activeBullets.removeIndex(i);bulletPool.free(item);}}}}
Pools類提供了動(dòng)態(tài)創(chuàng)建任何對(duì)象池的靜態(tài)方法。在上面的示例中,可以這樣使用:
作者:宋志輝?
出處:http://blog.csdn.net/song19891121
本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。?
支持:?新浪微博?騰訊微博
?
轉(zhuǎn)載于:https://www.cnblogs.com/hainange/archive/2013/05/27/6153548.html
總結(jié)
以上是生活随笔為你收集整理的第九章 Libgdx内存管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 又要说离开了!
- 下一篇: 全球首发5.5G 高通骁龙X75基带不再