我在Android应用中遇到内存泄漏问题。根据我的hprof分析,它似乎是由来自byte
类Bitmap
的{{1}}数组引起的。
以下是几个阵列之一的MAT分析:
和违规类的代码:
SurfaceView
public class AndroidFastRenderView extends SurfaceView implements Runnable {
AndroidGame game;
Bitmap framebuffer;
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;
public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) {
super(game);
this.game = game;
this.framebuffer = framebuffer;
this.holder = getHolder();
this.holder.setFormat(PixelFormat.RGBA_8888);
setSystemUiVisibility(
SYSTEM_UI_FLAG_IMMERSIVE_STICKY | SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
public void resume() {
setSystemUiVisibility(
SYSTEM_UI_FLAG_IMMERSIVE_STICKY | SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_HIDE_NAVIGATION);
running = true;
renderThread = new Thread(this);
renderThread.start();
}
public void run() {
Rect dstRect = new Rect();
long startTime = System.nanoTime();
while (running) {
if (!holder.getSurface().isValid())
continue;
float deltaTime = (System.nanoTime() - startTime) / 10000000.000f;
startTime = System.nanoTime();
game.getCurrentScreen().update(deltaTime);
game.getCurrentScreen().paint(deltaTime);
Canvas canvas = holder.lockCanvas();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(framebuffer, null, dstRect, null);
holder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
running = false;
while (true) {
try {
renderThread.join();
break;
} catch (InterruptedException e) {
}
}
}
public void release() {
getHolder().getSurface().release();
framebuffer.recycle();
}
}
在我的活动release()
中被调用。
onDestroy()
:
frameBuffer
在我的活动中。
答案 0 :(得分:-1)
确保您的线程“renderThread”已停止。如果它仍处于活动状态,它会保存对“AndroidFastRenderView”对象的引用,因此它无法释放内存...