android线程的内存大小,每日一问 | Android 中线程与内存之间相关问题

Android Profiler 的 CPU 中的THREADS 也能看到运行的线程

只了解到这些...

1.查看应用 进程 和 进程 对应的线程信息

//查看应用包名所在的进程信息

> adb shell ps -t | grep 包名

USER PID PPID VSIZE RSS WCHAN PC NAME

u0_a239 7499 569 1131304 53268 SyS_epoll_ 00000000 S com.lxk.test

//查看进程的所有线程 利用包名对应的 USER 值

//u0_a239 是根据包名查出的 USER 值

> adb shell ps -t | grep u0_a239

USER PID PPID VSIZE RSS WCHAN PC NAME

u0_a239 7499 569 1131304 53268 SyS_epoll_ 00000000 S com.lxk.test

u0_a239 7504 7499 1131304 53268 do_sigtime 00000000 S Signal Catcher

u0_a239 7505 7499 1131304 53268 poll_sched 00000000 S JDWP

u0_a239 7506 7499 1131304 53268 futex_wait 00000000 S ReferenceQueueD

u0_a239 7507 7499 1131304 53268 futex_wait 00000000 S FinalizerDaemon

u0_a239 7508 7499 1131304 53268 futex_wait 00000000 S FinalizerWatchd

u0_a239 7509 7499 1131304 53268 futex_wait 00000000 S HeapTaskDaemon

u0_a239 7510 7499 1131304 53268 binder_thr 00000000 S Binder_1

u0_a239 7511 7499 1131304 53268 binder_thr 00000000 S Binder_2

u0_a239 7514 7499 1131304 53268 futex_wait 00000000 S .1, thread No.1

u0_a239 7525 7499 1131304 53268 SyS_epoll_ 00000000 S RenderThread

u0_a239 7530 7499 1131304 53268 futex_wait 00000000 S hwuiTask1

u0_a239 7531 7499 1131304 53268 futex_wait 00000000 S hwuiTask2

u0_a239 11619 11600 1127052 48216 binder_thr 00000000 S Binder_3

2.每个线程会占据多少内存空间?这个内存空间是固定大小的吗?还是一开始很少,随着你方法调用越来越多,而动态增加?

Thread 类的方法有提供对应的方法

public Thread(ThreadGroup group, Runnable target, String name,

long stackSize) {

init(group, target, name, stackSize);

}

/**

* Initializes a Thread.

*

* @param g the Thread group

* @param target the object whose run() method gets called

* @param name the name of the new Thread

* @param stackSize the desired stack size for the new thread, or

* zero to indicate that this parameter is to be ignored.

*/

private void init(ThreadGroup g, Runnable target, String name, long stackSize)

public synchronized void start() {

try {

nativeCreate(this, stackSize, daemon);

started = true;

}

}

c层等一个大佬

测试 新开线程循环调用方法 会让App使用的内存越来越多。

线程私有的是 虚拟机栈 和 本地方法栈。

所有会随着方法的内部调用 入栈出栈 从而使空间的大小变化。

3.如何拿到一个线程已经使用的栈空间?

获取所有的堆栈信息

Thread.getAllStackTraces().size();

然后计算每一个 栈帧 的大小之和 ?

$ adb shell ps -t | grep 19119

USER PID PPID VSIZE RSS WCHAN PC NAME

u0_a239 19119 19080 1146632 65916 futex_wait 00000000 S 103style-161415

不确定这个 VSIZE 是不是线程所占的栈空间

VSIZE: 使用掉的内存大小

RSS: 占用的内存大小

VSIZE 固定不变, RSS会增大

所以 问题2 是固定大小???

4.我们能够调整一个线程占据的内存大小吗?

通过Thread的构造方法 配置的好像没有效果

等一个大佬

public class ThreadInfoActivity extends AppCompatActivity {

int count = 0;

long used = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_thread_info);

getMemoryInfo();

findViewById(R.id.add_thread).setOnClickListener(view -> {

count++;

new TThread().start();

getMemoryInfo();

});

}

private void getMemoryInfo() {

runOnUiThread(() -> {

Runtime mRuntime = Runtime.getRuntime();

long usedMemory = mRuntime.totalMemory() - mRuntime.freeMemory();

String result = "自己添加的线程数量:" + count

+ "\n Cur UsedMemory:" + usedMemory

+ "\n larger :" + (usedMemory - used)

+ "bytes;\n"

+ "---------------------------------\n";

used = usedMemory;

System.out.println(result);

});

}

void test() {

int a = 0;

int b = 1;

System.out.println(Thread.currentThread().getName() + " cur size = " + Thread.getAllStackTraces().size());

getMemoryInfo();

}

class TThread extends Thread {

public TThread() {

super(null, null, "103style-" + System.currentTimeMillis(), 10 * 1024 * 1024);

}

@Override

public void run() {

super.run();

while (true) {

try {

Thread.sleep(1000);

test();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

输出

System.out: 自己添加的线程数量:0

System.out: Cur UsedMemory:4980288

System.out: larger :4980288bytes;

System.out: ---------------------------------

System.out: 自己添加的线程数量:1

System.out: Cur UsedMemory:5340376

System.out: larger :360088bytes;

System.out: ---------------------------------

System.out: 103style-1614151214193 cur size = 15

System.out: 自己添加的线程数量:1

System.out: Cur UsedMemory:5401944

System.out: larger :61568bytes;

System.out: ---------------------------------

System.out: 103style-1614151214193 cur size = 15

System.out: 自己添加的线程数量:1

System.out: Cur UsedMemory:5415944

System.out: larger :14000bytes;

System.out: ---------------------------------

System.out: 103style-1614151214193 cur size = 15

System.out: 自己添加的线程数量:1

System.out: Cur UsedMemory:5425912

System.out: larger :9968bytes;

System.out: ---------------------------------

System.out: 103style-1614151465734 cur size = 18

System.out: 自己添加的线程数量:4

System.out: Cur UsedMemory:5513040

System.out: larger :13776bytes;

System.out: ---------------------------------

System.out: 103style-1614151214193 cur size = 18

System.out: 自己添加的线程数量:4

System.out: Cur UsedMemory:5530944

System.out: larger :17904bytes;

System.out: ---------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值