修复部分内存泄漏问题

This commit is contained in:
zlzw 2022-12-23 10:27:25 +08:00
parent af5d8dbf19
commit 33ee465a43
14 changed files with 152 additions and 42 deletions

View File

@ -17,6 +17,8 @@ import com.yunbao.common.http.CommonHttpUtil;
import com.yunbao.common.utils.L;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import io.rong.imlib.RongIMClient;
@ -33,6 +35,7 @@ public class CommonAppContext extends MultiDexApplication {
public static CommonAppContext sInstance;
public static WeakReference<Activity> activityWeakReference;
public static List<WeakReference<Activity>> activityList=new ArrayList<>();
private int mCount;
private boolean mFront;//是否前台
public static int jpushMsgNum;
@ -111,6 +114,7 @@ public class CommonAppContext extends MultiDexApplication {
jpushMsgNum = 0;
CommonAppConfig.getInstance().setFrontGround(true);
}
activityList.add(activityWeakReference);
}
@Override
@ -131,6 +135,12 @@ public class CommonAppContext extends MultiDexApplication {
L.e("AppContext------->处于后台");
CommonAppConfig.getInstance().setFrontGround(false);
}
for (WeakReference<Activity> reference : activityList) {
if (reference.get() == activity) {
activityList.remove(reference);
return;
}
}
}
@Override

View File

@ -40,7 +40,7 @@ public class JavascriptInterfaceUtils {
private boolean dialogClose = false;
//同名activity跳转问题
private boolean LiveZhuangBana = true;
private static JavascriptInterfaceUtils sInstance;
//private static JavascriptInterfaceUtils sInstance;
public JavascriptInterfaceUtils setPageClose(boolean pageClose) {
this.pageClose = pageClose;
@ -67,9 +67,9 @@ public class JavascriptInterfaceUtils {
synchronized (JavascriptInterfaceUtils.class) {
sInstance = new JavascriptInterfaceUtils();
// sInstance = new JavascriptInterfaceUtils();
return sInstance;
return new JavascriptInterfaceUtils();
}
}

View File

@ -13,7 +13,7 @@
#Thu Feb 04 00:05:45 CST 2021
android.injected.testOnly=false
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
android.useAndroidX=true

View File

@ -3,5 +3,5 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip

View File

@ -118,7 +118,6 @@ public class LiveAudienceActivity extends LiveActivity {
private static final String TAG = "LiveAudienceActivity";
public static String anyway;
public JSONObject pkInfo;
public static LiveAudienceActivity liveAudienceActivity;
// 竖直滑动 ViewPager
private VerticalViewPager verticalViewPager;
@ -166,7 +165,6 @@ public class LiveAudienceActivity extends LiveActivity {
protected void main() {
Bus.getOn(this);
super.main();
liveAudienceActivity = this;
CrashSaveBean.getInstance().addEnterRoom();
mProcessResultUtil = new ProcessResultUtil(this);
Intent intent = getIntent();

View File

@ -60,7 +60,7 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
private String roomId;
private DialogInterface.OnShowListener listener;
private TextView htmlError;
private boolean isSetHeight =false;
private boolean isSetHeight = false;
public LiveHDDialogFragment() {
@ -100,6 +100,7 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
window.setWindowAnimations(R.style.bottomToTopAnim);
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
isFullWindow = false;
if (isFullWindow) {
Display display = null;
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
@ -129,19 +130,26 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
@Override
public void onDetach() {
super.onDetach();
Bus.getOff(this);
Bus.getOff(this);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bus.getOn(this);
Bus.getOn(this);
}
LinearLayout layout;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView = (WebView) findViewById(R.id.rlWebview);
layout = (LinearLayout) findViewById(R.id.layout);
mWebView = new WebView(mContext);
ViewGroup.LayoutParams _params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mWebView.setLayoutParams(_params);
layout.addView(mWebView);
htmlError = (TextView) findViewById(R.id.html_error);
findViewById(R.id.btn_back).setOnClickListener(v -> dismiss());
mWebView.addJavascriptInterface(JavascriptInterfaceUtils.getInstance().setmContext(getActivity(), mWebView)
@ -174,7 +182,7 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
findViewById(R.id.title_laout).setVisibility(View.VISIBLE);
}
if (bundle.getInt("height", -1) != -1) {
isSetHeight =true;
isSetHeight = true;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, bundle.getInt("height", -1));
mWebView.setLayoutParams(params);
}
@ -195,7 +203,7 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
@Override
public void onPageFinished(WebView view, String url) {
if(!isSetHeight) {
if (!isSetHeight) {
view.loadUrl("javascript:window.androidObject.setHeight(document.body.clientHeight,document.body.clientWidth,true)");
}
if (listener != null) {
@ -230,7 +238,6 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
}
private void gotoLive(final String live_id) {
if (!TextUtils.equals(roomId, live_id)) {
LiveHttpUtil.getLiveInfo(live_id, new HttpCallback() {
@ -244,7 +251,7 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
if (liveBean == null) {
return;
}
if(MicStatusManager.getInstance().isMic(liveUid)){
if (MicStatusManager.getInstance().isMic(liveUid)) {
MicStatusManager.getInstance().showDownMicDialog(mContext);
return;
}
@ -306,7 +313,10 @@ public class LiveHDDialogFragment extends AbsDialogFragment {
public void onDestroy() {
super.onDestroy();
//结束webview的加载
mWebView.loadUrl("about:blank");
layout.removeView(mWebView);
mWebView.destroy();
mWebView = null;
}
@Subscribe(threadMode = ThreadMode.MAIN)

View File

@ -66,6 +66,7 @@ import java.util.Locale;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import pl.droidsonroids.gif.GifImageView;
/**
* Created by cxf on 2018/10/9.
@ -198,12 +199,23 @@ public class LiveAudienceViewHolder extends AbsLiveViewHolder {
super.init();
EventBus.getDefault().register(this);
findViewById(R.id.btn_close).setOnClickListener(this);
findViewById(R.id.btn_share).setOnClickListener(this);
GifImageView btn_share = (GifImageView) findViewById(R.id.btn_share);
btn_share.setImageResource(R.mipmap.zan_gif);
btn_share.setOnClickListener(this);
findViewById(R.id.btn_red_pack).setOnClickListener(this);
findViewById(R.id.btn_gift).setOnClickListener(this);
findViewById(R.id.btn_zg).setOnClickListener(this);
GifImageView btn_gift = (GifImageView) findViewById(R.id.btn_gift);
btn_gift.setImageResource(R.mipmap.live_lw);
btn_gift.setOnClickListener(this);
GifImageView btn_zg = (GifImageView) findViewById(R.id.btn_zg);
btn_zg.setImageResource(R.mipmap.zg);
btn_zg.setOnClickListener(this);
GifImageView onecz = (GifImageView) findViewById(R.id.img_onecz);
onecz.setImageResource(R.mipmap.onecz);
voiceButton = (LinearLayout) findViewById(R.id.voice_button);
giftImage = (ImageView) findViewById(R.id.gift_image);
giftImage.setImageResource(R.mipmap.live_lw);
liveNewPeople = (ImageView) findViewById(R.id.live_new_people);
svga_new_user_gif = (SVGAImageView) findViewById(R.id.svga_new_user_gif);
svga_new_user_double = (SVGAImageView) findViewById(R.id.svga_new_user_double);
@ -249,7 +261,9 @@ public class LiveAudienceViewHolder extends AbsLiveViewHolder {
});
}
});
findViewById(R.id.btn_hd).setOnClickListener(new View.OnClickListener() {//游戏活动按钮
GifImageView btn_hd = (GifImageView) findViewById(R.id.btn_hd);
btn_hd.setImageResource(R.mipmap.play);
btn_hd.setOnClickListener(new View.OnClickListener() {//游戏活动按钮
@Override
public void onClick(View view) {
LivePromotionDialogFragment fragment = new LivePromotionDialogFragment();

View File

@ -403,6 +403,16 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
mStarted = false;
if (mPlayer != null) {
mPlayer.stopPlay();
mPlayer = null;
}
if (mVideoView != null) {
mVideoView.clearLastFrame(true);
mVideoView.removeVideoView();
mVideoView = null;
}
if (leave != null) {
leave.setImageDrawable(null);
leave = null;
}
L.e(TAG, "release------->");
}

View File

@ -152,6 +152,7 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@ -457,6 +458,7 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
public void endDRGif() {
if (dr_pk_end_gif.getVisibility() == View.GONE) {
dr_pk_end_gif.setImageResource(R.mipmap.drpkend);
dr_pk_end_gif.setVisibility(View.VISIBLE);
RequestOptions options = new RequestOptions().skipMemoryCache(true);//配置
Glide.with(mContext).asGif()
@ -3601,6 +3603,7 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
LiveHttpUtil.cancel(LiveHttpConsts.GET_USER_LIST);
LiveHttpUtil.cancel(LiveHttpConsts.TIME_CHARGE);
CommonHttpUtil.cancel(CommonHttpConsts.SET_ATTENTION);
clearImageView();
}
@Subscribe(threadMode = ThreadMode.MAIN)
@ -3905,4 +3908,22 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
}
};
private void clearImageView() {
try {
Field[] fields = getClass().getFields();
int i = 0;
for (Field field : fields) {
field.setAccessible(true);
if (field.get(this) instanceof ImageView) {
((ImageView) field.get(this)).setImageDrawable(null);
i++;
}
}
ToastUtil.show("已清理:" + i);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -9,6 +10,7 @@
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
@ -36,7 +38,7 @@
android:onClick="backClick"
android:padding="9dp"
android:src="@mipmap/icon_back"
android:tint="@color/textColor" />
app:tint="@color/textColor" />
<ImageView
android:id="@+id/img_more"
@ -46,21 +48,21 @@
android:layout_centerVertical="true"
android:padding="9dp"
android:src="@mipmap/btn_more_black"
android:tint="@color/textColor"
android:visibility="gone" />
android:visibility="gone"
app:tint="@color/textColor" />
</RelativeLayout>
<TextView
android:id="@+id/html_error"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:visibility="gone" />
<WebView
<!-- <WebView
android:id="@+id/rlWebview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent" />-->
</LinearLayout>

View File

@ -29,7 +29,6 @@
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="@drawable/m_chu_xia"
app:riv_oval="true" />

View File

@ -52,8 +52,7 @@
android:id="@+id/img_onecz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="@mipmap/onecz" />
android:adjustViewBounds="true" />
</LinearLayout>
@ -144,7 +143,6 @@
android:adjustViewBounds="true"
android:background="@mipmap/img_bg"
android:padding="5dp"
android:src="@mipmap/zg"
android:visibility="gone" />
@ -166,7 +164,7 @@
android:layout_width="33dp"
android:layout_height="33dp"
android:adjustViewBounds="true"
android:src="@mipmap/play" />
/>
</RelativeLayout>
@ -181,7 +179,6 @@
android:adjustViewBounds="true"
android:background="@mipmap/img_bg"
android:padding="5dp"
android:src="@mipmap/zan_gif"
android:visibility="gone" />
<pl.droidsonroids.gif.GifImageView
@ -194,7 +191,6 @@
android:layout_toLeftOf="@id/btn_close"
android:adjustViewBounds="true"
android:background="@mipmap/img_bg"
android:src="@mipmap/live_lw"
android:visibility="gone" />
<ImageView
@ -394,8 +390,7 @@
android:id="@+id/gift_image"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginEnd="8dp"
android:src="@mipmap/live_lw" />
android:layout_marginEnd="8dp" />
<!--普通工具合集-->
<ImageView
android:id="@+id/total_image"

View File

@ -37,7 +37,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/m_chu_xia"
app:riv_oval="true" />
<ImageView
@ -654,7 +653,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/m_chu_xia"
app:riv_oval="true" />
</RelativeLayout>
@ -1265,7 +1263,6 @@
android:layout_alignTop="@+id/live_video"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="@mipmap/icon_rank_top_box"
android:visibility="gone" />
<RelativeLayout
@ -2041,7 +2038,7 @@
android:layout_marginEnd="1dp"
android:layout_marginBottom="11dp"
android:scaleType="centerCrop"
android:src="@drawable/m_chu_xia"
app:riv_oval="true" />
<ImageView
@ -2075,7 +2072,7 @@
android:layout_marginEnd="1dp"
android:layout_marginBottom="11dp"
android:scaleType="centerCrop"
android:src="@drawable/m_chu_xia"
app:riv_oval="true" />
<ImageView
@ -2109,7 +2106,7 @@
android:layout_marginEnd="1dp"
android:layout_marginBottom="11dp"
android:scaleType="centerCrop"
android:src="@drawable/m_chu_xia"
app:riv_oval="true" />
<ImageView
@ -2294,7 +2291,6 @@
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:layout_marginTop="160dp"
android:src="@mipmap/drpkend"
android:visibility="gone" />
<ImageView

View File

@ -1,14 +1,29 @@
package com.yunbao.main.views;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzf.easyfloat.EasyFloat;
import com.lzf.easyfloat.enums.ShowPattern;
import com.yunbao.common.CommonAppContext;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.utils.DeviceUtils;
import com.yunbao.common.utils.WordUtil;
import com.yunbao.main.R;
import org.greenrobot.eventbus.EventBus;
import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by cxf on 2018/9/22.
* MainActivity 首页 删除
@ -30,12 +45,52 @@ public class MainHomeViewHolder extends AbsMainHomeParentViewHolder {
return R.layout.view_main_home;
}
private TextView debugView;
private TimerTask task;
@Override
public void init() {
super.init();
img_trophy = (ImageView) findViewById(R.id.img_trophy);
ImgLoader.display(mContext, "https://downs.yaoulive.com/gif_trophy.gif", img_trophy);
img_trophy.setOnClickListener(v -> {
if (EasyFloat.isShow("Debug")) {
EasyFloat.dismiss("Debug");
task.cancel();
task = null;
return;
}
if (debugView == null) {
debugView = new TextView(mContext);
debugView.setBackgroundColor(Color.WHITE);
}
EasyFloat.with(mContext)
.setDragEnable(true)
.setTag("Debug")
.setShowPattern(ShowPattern.ALL_TIME)
.setLayout(debugView)
.show();
if (task == null) {
task = new TimerTask() {
Handler handler = new Handler(Looper.getMainLooper());
@Override
public void run() {
StringBuilder builder = new StringBuilder();
for (WeakReference<Activity> reference : CommonAppContext.activityList) {
builder.append(reference.get().getClass().getSimpleName()).append("\n");
}
builder.append("内存:").append(DeviceUtils.getMemory(mContext));
if (debugView != null) {
handler.post(() -> debugView.setText(builder.toString()));
}
}
};
new Timer().schedule(task, 0, 100);
}
});
}
@Override