/** * Turn on the thread detection software available memory condition, after the software exits, call System.exit (0) The thread will stop * * * @param sleep * Detect memory every time * @pa RAM Standard * Memory over how many proportional alarms */public static void Memorywatch (final int sleep, Final int. standard) {if (sleep <= 0) {log.e (TAG, "memorywatch, invalid sleep = 0!"); Return } if (standard <= 0 && standard > +) {log.e (TAG, "Memorywatch, invalid standard Percent = "+ standard"; Return } New Thread (new Runnable () {@Override public void run () { LOG.D (TAG, "Memorywatch, start thread!"); while (true) {long divisor = 1024 * 1024; Long MaxMem = Runtime.getruntime (). MaxMemory (); Long Totalmem = Runtime.getruntime (). TotalMemory (); Long Freemem = Runtime.getruntime (). Freememory (); int percent = (int) (TOTALMEM * 100/maxmem); LOG.D (TAG, "Memorywatch" + ", app MaxMem =" + Maxmem/divisor + "M, Totalmem =" + Totalmem/ Divisor + "m, Freemem =" + Freemem/divisor + "m, percent =" + P ercent); If (standard <= percent) {if (Freemem/divisor <= 2) {LOG.E (TAG, "Memorywatch" + ", app MaxMem =" + MaxMem /divisor + "m, Totalmem =" + Totalmem/divisor + "m, Freemem =" + Freemem/divisor + "M, percent =" + percent); } else if (Freemem/divisor >= totalmem/(DIVISOR * 2)) { LOG.E (TAG, "MemoRywatch "+", app MaxMem = "+ Maxmem/divisor +" M, Totalmem = " + Totalmem/divisor + "m, Freemem =" + Freemem/divisor + "m, percent = "+ percent +"% "+", reset memory warn flag = t Rue! ");}} try {thread.sleep (sleep); } catch (Interruptedexception e) {log.e (TAG, "Memorywatch, T Hread Sleep Exception: "+ E); }}}). Start (); }
Android Memory Detection