package com.lyyd.debugmemory;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
/**
* Handle demo test
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView pss, pssNum, javaHeap, javaHeapNum, nativeHeap, nativeHeapNum;
Button getHeapBtn, startBtn, stopBtn, pauseBtn;
ProgressBar progressBar;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pss = findViewById(R.id.pss);
pssNum = findViewById(R.id.pssNum);
javaHeap = findViewById(R.id.javaHeap);
javaHeapNum = findViewById(R.id.javaHeapNum);
nativeHeap = findViewById(R.id.nativeHeap);
nativeHeapNum = findViewById(R.id.nativeHeapNum);
getHeapBtn = findViewById(R.id.getHeap);
startBtn = findViewById(R.id.start);
pauseBtn = findViewById(R.id.pause);
stopBtn = findViewById(R.id.stop);
progressBar = findViewById(R.id.progressBar);
getHeapBtn.setOnClickListener(this);
startBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
}
private float[] getMemoryData(int pid) {
float pssHeap = 0.0F, javaHeap = 0.0F, nativeHeap = 0.0F;
try {
Debug.MemoryInfo memoryInfo = null;
if (Build.VERSION.SDK_INT > 28) {
memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
} else {
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
Debug.MemoryInfo[] memoryInfos = am.getProcessMemoryInfo(new int[]{pid});
if (memoryInfos != null && memoryInfos.length > 0) {
memoryInfo = memoryInfos[0];
}
}
int totalPss = memoryInfo.getTotalPss();
int java = memoryInfo.dalvikPss;
int nativeNum = memoryInfo.nativePss;
if (totalPss >= 0) {
pssHeap = totalPss / 1024.0F;
}
if (java >= 0) {
javaHeap = java / 1024.0F;
}
if (nativeNum >= 0) {
nativeHeap = nativeNum / 1024.0F;
}
Log.d("HeapSize", "pss:" + pssHeap + "Kb java:" + javaHeap + "Kb native:" + nativeHeap + "Kb");
} catch (Exception e) {
e.printStackTrace();
}
return new float[]{pssHeap, javaHeap, nativeHeap};
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.getHeap) {
if (getHeapBtn.getText().equals("获取内存")) {
getHeapBtn.setText("停止获取");
pssNum.setText("");
javaHeapNum.setText("");
nativeHeapNum.setText("");
memoryHandler.post(memoryThread);
Toast.makeText(this, "开始采集数据", Toast.LENGTH_LONG).show();
} else {
getHeapBtn.setText("获取内存");
memoryHandler.removeCallbacks(memoryThread);
Toast.makeText(this, "停止采集数据", Toast.LENGTH_LONG).show();
}
} else if (v.getId() == R.id.start) {
i = 0;
updateBarHandler.post(updateThread);
} else if (v.getId() == R.id.pause) {
updateBarHandler.removeCallbacks(updateThread);
} else if (v.getId() == R.id.stop) {
updateBarHandler.removeCallbacks(updateThread);
progressBar.setProgress(0);
i = 0;
}
}
Handler memoryHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
float[] mems = getMemoryData(120);
pssNum.setText(String.valueOf(mems[0]) + "Kb");
javaHeapNum.setText(String.valueOf(mems[1]) + "Kb");
nativeHeapNum.setText(String.valueOf(mems[2]) + "Kb");
memoryHandler.post(memoryThread);
}
}
};
Runnable memoryThread = new Runnable() {
@Override
public void run() {
memoryHandler.sendEmptyMessage(0x123);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Handler updateBarHandler = new Handler() {
public void handleMessage(Message msg) {
progressBar.setProgress(msg.arg1);
updateBarHandler.post(updateThread);
}
};
Runnable updateThread = new Runnable() {
@Override
public void run() {
i += 3;
Message msg = updateBarHandler.obtainMessage();
msg.arg1 = i;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i > 100) {
updateBarHandler.removeCallbacks(updateThread);
} else {
updateBarHandler.sendMessage(msg);
}
}
};
}