Android获取当前App已经使用java heap大小

Runtime runtime = Runtime.getRuntime();

Getting the max heap size that the app can use:

//获取可以使用java heap最大值,单位byte
long maxMemory=runtime.maxMemory();

Getting how much of the heap your app can now use (not use memory) :

//获取**申请但未使用**java heap,单位byte
long freeMemory = runtime.freeMemory();
//获取已经申请的全部内存
long usedMemory=runtime.totalMemory() ;

Getting how much of the heap your app currently uses:

//获取当前已经使用java heap,单位byte
long usedMemory=runtime.totalMemory() - runtime.freeMemory();
//当前可以new出来的最大内存,单位byte
long availableMemory = runtime.maxMemory() - runtime.totalMemory()
                    + runtime.freeMemory();

And, to format each of them nicely, you can use:

 String formattedMemorySize=Formatter.formatShortFileSize(context,memorySize);

*[1]:https://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值