Skip to content

Commit

Permalink
修复内存泄漏问题
Browse files Browse the repository at this point in the history
  • Loading branch information
JinJieGu committed Jul 31, 2017
1 parent 57a8b27 commit db2c796
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 28 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(path: ':library')
compile 'com.github.Jay-Goo:AndroidMP3Recorder:v1.0.7'

//内存泄漏检测
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="jaygoo.wavelineview">

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -17,6 +18,10 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name=".LeakTestActivity"
>
</activity>
</application>

</manifest>
25 changes: 25 additions & 0 deletions app/src/main/java/jaygoo/wavelineview/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package jaygoo.wavelineview;

import android.app.Application;

import com.squareup.leakcanary.LeakCanary;


/**
* ================================================
* 作 者:JayGoo
* 版 本:
* 创建日期:2017/7/28
* 描 述:
* ================================================
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
return;
}
LeakCanary.install(this);
}
}
69 changes: 69 additions & 0 deletions app/src/main/java/jaygoo/wavelineview/LeakTestActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package jaygoo.wavelineview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import jaygoo.widget.wlv.WaveLineView;

/**
* ================================================
* 作 者:JayGoo
* 版 本:
* 创建日期:2017/7/31
* 描 述:
* ================================================
*/
public class LeakTestActivity extends Activity{
private WaveLineView waveLineView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
waveLineView = (WaveLineView) findViewById(R.id.waveLineView);

findViewById(R.id.startBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
waveLineView.startAnim();
}
});

findViewById(R.id.stopBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
waveLineView.stopAnim();

}
});

findViewById(R.id.leakTestBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
startActivity(new Intent(LeakTestActivity.this,MainActivity.class));
}
});
}


@Override
protected void onResume() {
super.onResume();
waveLineView.onResume(true);
}

@Override
protected void onPause() {
super.onPause();
waveLineView.onPause();
}

@Override
protected void onDestroy() {
super.onDestroy();
waveLineView.release();
}
}
16 changes: 13 additions & 3 deletions app/src/main/java/jaygoo/wavelineview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jaygoo.wavelineview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import jaygoo.widget.wlv.WaveLineView;
Expand All @@ -27,6 +29,15 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
waveLineView.stopAnim();

}
});

findViewById(R.id.leakTestBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
startActivity(new Intent(MainActivity.this,LeakTestActivity.class));
}
});
}
Expand All @@ -35,14 +46,13 @@ public void onClick(View v) {
@Override
protected void onResume() {
super.onResume();
waveLineView.onResume(true);
waveLineView.onResume(true);
}

@Override
protected void onPause() {
super.onPause();
waveLineView.onPause();

waveLineView.onPause();
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@
android:layout_height="wrap_content"
android:text="stop"
/>

<Button
android:id="@+id/leakTestBtn"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="Memory Leak Test"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
62 changes: 38 additions & 24 deletions library/src/main/java/jaygoo/widget/wlv/RenderView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.lang.ref.WeakReference;
import java.util.List;
/**
* ================================================
Expand All @@ -33,31 +33,37 @@ public RenderView(Context context, AttributeSet attrs, int defStyleAttr) {
getHolder().addCallback(this);
}

/*回调/线程*/

private class RenderThread extends Thread {
/*回调/线程*/
private static class RenderThread extends Thread {

private static final long SLEEP_TIME = 16;

private SurfaceHolder surfaceHolder;
private WeakReference<RenderView> renderView;
private boolean running = false;
private boolean destoryed = false;
private boolean isPause = false;
public RenderThread(SurfaceHolder holder) {
public RenderThread(RenderView renderView) {
super("RenderThread");
surfaceHolder = holder;
this.renderView = new WeakReference<>(renderView);
}

private SurfaceHolder getSurfaceHolder(){
if (getRenderView() != null ){
return getRenderView().getHolder();
}
return null;
}

private RenderView getRenderView(){
return renderView.get();
}

@Override
public void run() {
long startAt = System.currentTimeMillis();
while (true) {
while (!destoryed) {
synchronized (surfaceLock) {

if (destoryed){
return;
}

//这里并没有真正的结束Thread,防止部分手机连续调用同一Thread出错
while (isPause){
try {
Expand All @@ -68,28 +74,36 @@ public void run() {
}

if (running) {
Canvas canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
render(canvas, System.currentTimeMillis() - startAt); //这里做真正绘制的事情
surfaceHolder.unlockCanvasAndPost(canvas);
if (getSurfaceHolder() != null && getRenderView() != null) {
Canvas canvas = getSurfaceHolder().lockCanvas();
if (canvas != null) {
getRenderView().render(canvas, System.currentTimeMillis() - startAt); //这里做真正绘制的事情
getSurfaceHolder().unlockCanvasAndPost(canvas);
}
}else {
running = false;
}
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

}


public void setRun(boolean isRun) {
this.running = isRun;
}

}

private final Object surfaceLock = new Object();
private final static Object surfaceLock = new Object();
private RenderThread renderThread;

@Override
Expand All @@ -99,7 +113,7 @@ public void surfaceCreated(SurfaceHolder holder) {
throw new IllegalStateException();
}

renderThread = new RenderThread(holder);
renderThread = new RenderThread(this);
}

/**
Expand Down Expand Up @@ -220,10 +234,10 @@ public boolean isRunning(){

//释放相关资源,防止内存泄漏
public void release(){
stopAnim();
if (getHolder() != null && getHolder().getSurface() != null) {
getHolder().getSurface().release();
getHolder().removeCallback(this);
}
}

}

0 comments on commit db2c796

Please sign in to comment.