- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 android 中创建游戏,我注意到该游戏存在内存泄漏。我设法将内存泄漏隔离到一个较小的应用程序中,这样我就可以很好地了解如何解决它。
该应用程序为其 View 使用表面 View ,并附加了一个线程,以便将所有绘图绘制到屏幕上。当我开始一项新 Activity 并关闭我当前正在使用的 Activity 时,就会发生内存泄漏。当我在我的测试应用程序上进行内存转储时,我可以看到这一点,因为它所做的只是打开和关闭一个 Activity ( Activity a -> Activity b -> Activity a)。关于如何解决这个问题,我有点想不通了,因为我试图让我对 View (在线程内)创建的所有引用都无效,当我销毁 View 时,我尝试从表面 View 中删除回调,而且在 Activity 中,它似乎没有任何区别。
内存泄漏 Activity .java
package memory.leak;
import memory.leak.view.MemoryLeak;
import android.app.Activity;
import android.os.Bundle;
public class MemoryLeakActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MemoryLeak(this));
}
}
MemoryLeakViewThread.java
package memory.leak.thread;
import memory.leak.view.MemoryLeak;
import android.view.SurfaceHolder;
import android.graphics.Canvas;
public class MemoryLeakViewThread extends Thread {
private MemoryLeak view;
private boolean run =false;
public MemoryLeakViewThread(MemoryLeak view) {
this.view =view;
}
public void setRunning(boolean run) {
this.run =run;
}
@Override
public void run() {
Canvas canvas =null;
SurfaceHolder holder =this.view.getHolder();
while(this.run) {
canvas =holder.lockCanvas();
if(canvas !=null) {
this.view.onDraw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
holder =null;
this.view =null;
}
}
内存泄漏.java
package memory.leak.view;
import memory.leak.TestActivity;
import memory.leak.thread.MemoryLeakViewThread;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.GestureDetector.OnGestureListener;
public class MemoryLeak extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener {
private GestureDetector gesture;
private MemoryLeakViewThread vThread;
private Context context;
public MemoryLeak(Context context) {
super(context);
this.getHolder().addCallback(this);
this.vThread =new MemoryLeakViewThread(this);
this.gesture =new GestureDetector(this);
this.context =context;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
public void surfaceCreated(SurfaceHolder holder) {
if(!this.vThread.isAlive()) {
this.vThread =new MemoryLeakViewThread(this);
this.vThread.setRunning(true);
this.vThread.start();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
if(this.vThread.isAlive()) {
this.vThread.setRunning(false);
while(retry) {
try {
this.vThread.join();
retry =false;
} catch(Exception ee) {}
}
}
this.vThread =null;
this.context =null;
}
public boolean onTouchEvent(MotionEvent event) {
return this.gesture.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Intent helpScreenIntent =new Intent(this.context, TestActivity.class);
this.context.startActivity(helpScreenIntent);
if (this.context instanceof Activity)
((Activity) this.context).finish();
return true;
}
}
测试 Activity .java
package memory.leak;
import memory.leak.view.Test;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Test(this));
}
}
TestViewThread.java
package memory.leak.thread;
import memory.leak.view.Test;
import android.view.SurfaceHolder;
import android.graphics.Canvas;
public class TestViewThread extends Thread {
private Test panel;
private boolean run =false;
public TestViewThread(Test panel) {
this.panel =panel;
}
public void setRunning(boolean run) {
this.run =run;
}
@Override
public void run() {
Canvas canvas =null;
SurfaceHolder holder =this.panel.getHolder();
while(this.run) {
canvas =holder.lockCanvas();
if(canvas !=null) {
this.panel.onDraw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
holder =null;
this.panel =null;
}
}
测试.java
package memory.leak.view;
import memory.leak.MemoryLeakActivity;
import memory.leak.thread.TestViewThread;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.GestureDetector.OnGestureListener;
public class Test extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener {
private GestureDetector gesture;
private TestViewThread vThread;
private Context context;
public Test(Context context) {
super(context);
this.getHolder().addCallback(this);
this.vThread =new TestViewThread(this);
this.gesture =new GestureDetector(this);
this.context =context;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
public void surfaceCreated(SurfaceHolder holder) {
if(!this.vThread.isAlive()) {
this.vThread =new TestViewThread(this);
this.vThread.setRunning(true);
this.vThread.start();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
if(this.vThread.isAlive()) {
this.vThread.setRunning(false);
while(retry) {
try {
this.vThread.join();
retry =false;
} catch(Exception ee) {}
}
}
this.vThread =null;
this.context =null;
}
public boolean onTouchEvent(MotionEvent event) {
return this.gesture.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.RED);
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Intent helpScreenIntent =new Intent(this.context, MemoryLeakActivity.class);
this.context.startActivity(helpScreenIntent);
if (this.context instanceof Activity)
((Activity) this.context).finish();
return true;
}
}
--编辑--我对 View 类的 surfaceDestroyed(SurfaceHolder holder) 进行了更改,以便在告知线程停止时将线程必须设置为 null 的 View 。我所做的更改是
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
if(this.vThread.isAlive()) {
this.vThread.setRunning(false);
while(retry) {
try {
this.vThread.join();
retry =false;
} catch(Exception ee) {}
}
this.vThread.setRunning(false, null);
}
this.vThread =null;
this.context =null;
this.gesture =null;
}
您还需要将 surfaceCreated(SurfaceHolder holder) 方法更改为
public void surfaceCreated(SurfaceHolder holder) {
if(!this.vThread.isAlive()) {
this.vThread =new MemoryLeakViewThread();
this.vThread.setRunning(true, this);
this.vThread.start();
}
}
然后在线程类上我们需要更改以下内容
public MemoryLeakViewThread() {
}
public void setRunning(boolean run) {
this.run =run;
}
public void setRunning(boolean run, MemoryLeak view) {
this.run =run;
this.view =view;
}
这样做似乎解决了问题,现在唯一的问题是由于线程类和线程组,线程似乎停留在内存中。但我认为这可能是调试器的问题。
最佳答案
在 onSurfaceCreated 中创建线程时,不应在构造函数中创建新线程。将您的代码与我的示例进行比较:How can I use the animation framework inside the canvas?
关于Android Surfaceview 线程和内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7227624/
我正在使用两个 MjpegView(扩展 SurfaceView 的自定义 View )构建一个应用程序。问题是有时我看不到第二个摄像机 View ,因为它位于第一个摄像机 View 后面。流媒体工作
我有一个自定义相机应用程序,我希望任何预览尺寸都以全屏模式显示,而不会拉伸(stretch)相机预览图像。为此,我需要使surfaceView 大于屏幕以保持纵横比,因此实际上用户看到的比相机实际捕获
我收到从 SurfaceView.onAttachedToWindow 引发的异常。看起来 SurfaceView 正在尝试引用 mParent,但它是 null。有谁知道为什么不设置父级但会调用 o
我已经将 SurfaceView 子类化并在 Activity 的 onCreate 中实例化它。生成了预览,但控件永远不会进入 onDraw(),它在 SurfaceView 的子类中被重写。这是为
我的应用在 Android 8.0 上遇到了一些奇怪的问题。我有自己的可滚动小部件,代码是 available on github .它有两个 child ,可以无限期地逐一滚动。 在屏幕上,棋盘是一
我用两种方式编写了同一个程序。 一个使用 Surfaceview,另一个使用自定义 View 。根据 android SDK 开发指南,使用表面 View 更好,因为您可以生成一个单独的线程来处理图形
我想做的是 在开始播放视频之前在 SurfaceView 上显示背景图像。 我尝试只绘制一个 jpeg 图像作为 SurfaceView 的背景。有效。 我还尝试在 SurfaceView 上播放视频
我遇到了讨厌的 Android SurfaceView 行为: 当你离开一个带有 SurfaceView 的 Activity 并快速返回时,SurfaceView 之前的内容并没有消失,而是显示在新
我正在创建一个 FrameLayout 类型的布局,我在其中添加了两个 View 。两个 View 分别是 GLSurfaceView 和 SurfaceView 的对象。根据有关 SurfaceVi
我有一个 Activity 调用一个扩展 SurfaceView 并实现 Runnable 的类并将 Activity 类的 contentView() 设置为 surfaceview 类的实例。最小
由于某种原因,我的程序中的摄像头画面是横向的。如果我从底部将手指放在相机上,它会在表面 View 中显示为来自右侧。 我有一个非常标准的实现 surfaceView = (SurfaceVie
我想在我的 SurfaceView 顶部添加一个自定义 ImageButton,这样它基本上可以随 View 滚动。 Canvas 使用矩阵滚动和缩放。我该怎么做呢?谢谢。 最佳答案 假设我正确理解了
在 SurfaceView 上绘图时,我在拦截“后退”按键时遇到问题。我的 onKeyDown 事件似乎只在第二次和随后的按键事件中被调用——这对后退键没有用,因为 Activity 已经暂停或终止。
在我正在进行的项目中,我决定使用 SurfaceView 而不是自定义双缓冲介质。它提供了我需要的一切,而且它已经是双缓冲的。 问题是它不会让我指定多个脏矩形来重绘。 SurfaceView.lock
我正在创建一个小游戏我有一个 Activity : GameActivity.java: public class GameActivity extends Activity { public
这很可能是一件微不足道的事情,但我找不到解决方案。事情很简单,用户按下屏幕,当他的手指放在屏幕上时,我想要执行一段代码。 这是一个代码示例: @Override public boolean
我正在使用 Android SurfaceView 编写测试应用程序。我想看看这是否是一种可行的 2D 游戏开发方法。目前性能不是太好。 似乎每隔 15 秒左右就会出现微小的抖动和减速。我只在屏幕上画
我有一个 SurfaceView,我在其中显示了一个比 SurfaceView 的实际区域大得多的位图,因此我实现了一种方法,让用户可以在位图上滑动手指并上下移动它。这样做时,我希望能够显示垂直滚动条
我正在向 SurfaceView 提供的 Canvas 绘制一些自定义内容。这包含在具有其他 View 的 Activity 中。 我的 Activity 中的一个状态需要隐藏 SurfaceView
所以我在 Android Studio 中做的项目中一直遇到 NullPointerException。我需要让图像显示在我称为 DrawSurface 的自定义类中。我似乎找不到与我的问题相关的任何
我是一名优秀的程序员,十分优秀!