i using android studio 1.5.1 targeting android api 18 (before android kitkat 4.4, i’m dealing dalvik, not art runtime).
my questions are:
- how call hidden android method gettotaluss() using java reflection?
- if not possible, how find current process uss (unique set size) memory programmatically?
i trying use code below getting compiler error "unexpected token" @ statement labelled //error! below in code.
activitymanager activitymanager = (activitymanager) this.getsystemservice(activity_service); activitymanager.memoryinfo memoryinfo = new activitymanager.memoryinfo(); activitymanager.getmemoryinfo(memoryinfo); list<activitymanager.runningappprocessinfo> runningappprocesses = activitymanager.getrunningappprocesses(); map<integer, string> pidmap = new treemap<integer, string>(); (activitymanager.runningappprocessinfo runningappprocessinfo : runningappprocesses) { pidmap.put(runningappprocessinfo.pid, runningappprocessinfo.processname); } collection<integer> keys = pidmap.keyset(); int id= android.os.process.mypid(); for(int key : keys) { if (key != id) continue; int pids[] = new int[1]; int uss; pids[0] = key; android.os.debug.memoryinfo[] memoryinfoarray = activitymanager.getprocessmemoryinfo(pids); for(android.os.debug.memoryinfo pidmemoryinfo: memoryinfoarray) { try { class c; c = class.forname("android.app.activitymanager"); method m = c.getmethod("gettotaluss", null); uss = m.invoke(null,int); // << == error! } catch (classnotfoundexception e) { } catch (nosuchmethodexception e) { } catch (illegalaccessexception e) { } system.out.println("** uss = " + uss);
you're calling getmethod() on activitymanager class doesn't contain gettotaluss() method. that's why you're getting error.
instead, you'll want reflectively method memoryinfo class has gettotaluss() method. you'll want sure pass pidmemoryinfo receiver invoke() call. it's calling pidmemoryinfo.gettotaluss().
method m = android.os.debug.memoryinfo.class.getmethod("gettotaluss", null); totaluss = (int) m.invoke(pidmemoryinfo,(object[]) null);
see documentation on method's invoke() more details.
Comments
Post a Comment