티스토리 뷰
아래 함수를 사용하면 자바 및 네이티브 힙의 크기 및 사용량을 체크할 수 있다.
DDMS에서 체크 가능한 Java Heap에는 문제가 없는데도 불구하고
OutOfMemoryError가 발생하면 이 함수를 이용해 Heap사용량을 먼저 체크하자.
// for check heap size
public static void logHeap(Class clazz) {
final int MEMORY_BUFFER_LIMIT_FOR_RESTART = 15;
Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));
Double available = new Double(Debug.getNativeHeapSize())/new Double((1048576));
Double free = new Double(Debug.getNativeHeapFreeSize())/new Double((1048576));
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
Log.d(TAG, "debug. =================================");
Log.d(TAG, "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free) in [" + clazz.getName().replaceAll("com.asktingting.android.","") + "]");
Log.d(TAG, "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");
System.gc();
// don't need to add the following lines, it's just an app specific handling in my app
/*
if (allocated>=(new Double(Runtime.getRuntime().maxMemory())/new Double((1048576))-MEMORY_BUFFER_LIMIT_FOR_RESTART)) {
android.os.Process.killProcess(android.os.Process.myPid());
}
*/
}
public static void logHeap(Class clazz) {
final int MEMORY_BUFFER_LIMIT_FOR_RESTART = 15;
Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));
Double available = new Double(Debug.getNativeHeapSize())/new Double((1048576));
Double free = new Double(Debug.getNativeHeapFreeSize())/new Double((1048576));
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
Log.d(TAG, "debug. =================================");
Log.d(TAG, "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free) in [" + clazz.getName().replaceAll("com.asktingting.android.","") + "]");
Log.d(TAG, "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");
System.gc();
// don't need to add the following lines, it's just an app specific handling in my app
/*
if (allocated>=(new Double(Runtime.getRuntime().maxMemory())/new Double((1048576))-MEMORY_BUFFER_LIMIT_FOR_RESTART)) {
android.os.Process.killProcess(android.os.Process.myPid());
}
*/
}
'Development & Tips > Android' 카테고리의 다른 글
강제로 Exception 던지기 (0) | 2011.11.14 |
---|---|
[TIP] Wake Lock 컨트롤 하는 방법 (0) | 2011.03.11 |
[TIP] 스크린이 켜졌는지 확인하는 방법 (0) | 2011.03.11 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 논문주제
- tangible interface
- 렌더몽키
- 크루스컬
- C#
- 크라이텍
- Prim
- Game
- mst
- CryTek
- 셰이더
- Render Monkey
- Kruscal
- 소스
- 미디어랩
- 머지소트
- Algorithm
- Ambient Occlusion
- source code
- thesis
- 충남 안면도
- 퀵소트
- Sort
- 프림
- DirectX
- Exporter
- 알고리즘
- Android
- mail notifier
- 수묵렌더링
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함