Android位图/字节数组内存泄漏

时间:2015-11-18 04:37:20

标签: java android memory-leaks bitmap

我在Android应用中遇到内存泄漏问题。根据我的hprof分析,它似乎是由来自byteBitmap的{​​{1}}数组引起的。

以下是几个阵列之一的MAT分析:

MAT Analysis

和违规类的代码:

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

在我的活动中。

1 个答案:

答案 0 :(得分:-1)

确保您的线程“renderThread”已停止。如果它仍处于活动状态,它会保存对“AndroidFastRenderView”对象的引用,因此它无法释放内存...