User Tools

Site Tools


android:mem-leak

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android:mem-leak [2011/10/21 16:32] percyandroid:mem-leak [2016/05/05 13:07] (current) – external edit 127.0.0.1
Line 45: Line 45:
  
  
-====== 打印内存代码 ======+======代码 ====== 
 +===== 打印内存代码 ===== 
 + 
 +<code lang="java"> 
 +    private void printMemInfo(String TAG) { 
 +        TAG += ":meminfo"; 
 +        logI(TAG, "printMemInfo:"); 
 + 
 +        logI(TAG, "Max heap size = " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M"); 
 +        logI(TAG, "Allocate heap size = " + android.os.Debug.getNativeHeapAllocatedSize() / 1024 + "K"); 
 + 
 +        Method _readProclines = null; 
 +        try { 
 +            Class procClass; 
 +            procClass = Class.forName("android.os.Process"); 
 +            Class parameterTypes[] = new Class[]{String.class, String[].class, long[].class}; 
 +            _readProclines = procClass.getMethod("readProcLines", parameterTypes); 
 +            Object arglist[] = new Object[3]; 
 +            final String[] mMemInfoFields = new String[]{"MemTotal:", "MemFree:", "Buffers:", "Cached:"}; 
 +            long[] mMemInfoSizes = new long[mMemInfoFields.length]; 
 +            mMemInfoSizes[0] = 30; 
 +            mMemInfoSizes[1] = -30; 
 +            arglist[0] = new String("/proc/meminfo"); 
 +            arglist[1] = mMemInfoFields; 
 +            arglist[2] = mMemInfoSizes; 
 +            if (_readProclines != null) { 
 +                _readProclines.invoke(null, arglist); 
 +                for (int i = 0; i < mMemInfoSizes.length; i++) { 
 +                    logI(TAG, mMemInfoFields[i] + " = " + mMemInfoSizes[i] / 1024 + "M"); 
 +                } 
 +            } 
 +        } catch (ClassNotFoundException e) { 
 +            e.printStackTrace(); 
 +        } catch (SecurityException e) { 
 +            e.printStackTrace(); 
 +        } catch (IllegalArgumentException e) { 
 +            e.printStackTrace(); 
 +        } catch (IllegalAccessException e) { 
 +            e.printStackTrace(); 
 +        } catch (InvocationTargetException e) { 
 +            e.printStackTrace(); 
 +        } catch (NoSuchMethodException e) { 
 +            e.printStackTrace(); 
 +        } 
 +    } 
 +</code> 
 +===== unbindDrawables===== 
 +  unbindDrawables(activity.findViewById(R.id.root_view)); 
 +  System.gc(); 
 + 
 +<code> 
 +    /** 
 +     * Unbind all the drawables. 
 +     * @param view 
 +     */ 
 +    private void unbindDrawables(View view) { 
 +        if (view != null) { 
 +            if (view.getBackground() != null) { 
 +                view.getBackground().setCallback(null); 
 +            } 
 + 
 +            if (view instanceof ViewGroup && !(view instanceof AdapterView)) { 
 +                for (int i 0; i < ((ViewGroup) view).getChildCount(); i++) { 
 +                    unbindDrawables(((ViewGroup) view).getChildAt(i)); 
 +                } 
 +                ((ViewGroup) view).removeAllViews(); 
 +            } 
 +        } 
 +    } 
 +</code> 
  
/var/www/dokuwiki/wiki/data/attic/android/mem-leak.1319185967.txt.gz · Last modified: 2016/05/05 13:06 (external edit)