android:mem-leak
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| android:mem-leak [2011/10/21 16:31] – percy | android:mem-leak [2016/05/05 13:07] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 43: | Line 43: | ||
| - [[http:// | - [[http:// | ||
| + | |||
| + | |||
| + | ======代码 ====== | ||
| + | ===== 打印内存代码 ===== | ||
| + | |||
| + | <code lang=" | ||
| + | private void printMemInfo(String TAG) { | ||
| + | TAG += ": | ||
| + | logI(TAG, " | ||
| + | |||
| + | logI(TAG, "Max heap size = " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + " | ||
| + | logI(TAG, " | ||
| + | |||
| + | Method _readProclines = null; | ||
| + | try { | ||
| + | Class procClass; | ||
| + | procClass = Class.forName(" | ||
| + | Class parameterTypes[] = new Class[]{String.class, | ||
| + | _readProclines = procClass.getMethod(" | ||
| + | Object arglist[] = new Object[3]; | ||
| + | final String[] mMemInfoFields = new String[]{" | ||
| + | long[] mMemInfoSizes = new long[mMemInfoFields.length]; | ||
| + | mMemInfoSizes[0] = 30; | ||
| + | mMemInfoSizes[1] = -30; | ||
| + | arglist[0] = new String("/ | ||
| + | arglist[1] = mMemInfoFields; | ||
| + | arglist[2] = mMemInfoSizes; | ||
| + | if (_readProclines != null) { | ||
| + | _readProclines.invoke(null, | ||
| + | for (int i = 0; i < mMemInfoSizes.length; | ||
| + | logI(TAG, mMemInfoFields[i] + " = " + mMemInfoSizes[i] / 1024 + " | ||
| + | } | ||
| + | } | ||
| + | } 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(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ===== unbindDrawables===== | ||
| + | unbindDrawables(activity.findViewById(R.id.root_view)); | ||
| + | System.gc(); | ||
| + | |||
| + | < | ||
| + | /** | ||
| + | * 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(); | ||
| + | unbindDrawables(((ViewGroup) view).getChildAt(i)); | ||
| + | } | ||
| + | ((ViewGroup) view).removeAllViews(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
/var/www/dokuwiki/wiki/data/attic/android/mem-leak.1319185909.txt.gz · Last modified: 2016/05/05 13:06 (external edit)
