이 튜토리얼에서는 안드로이드 앱이 사용하는 RAM(메모리)의 양을 확인하는 방법을 단계별로 알아봅니다. ActivityManager와 Runtime 클래스를 활용하면 기기의 가용 메모리, 전체 메모리, 그리고 앱 자체의 메모리 사용 현황까지 손쉽게 조회할 수 있습니다.
1단계: 새 프로젝트 생성
Android Studio를 실행한 뒤 File → New Project를 선택하고, 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml에 다음 코드를 추가합니다. 화면 중앙에 메모리 정보를 표시할 TextView 하나를 배치하는 간단한 구조입니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:textStyle="bold"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
3단계: MainActivity 구현
src/MainActivity.java에 다음 코드를 추가합니다. getMemoryInfo() 메서드가 시스템 및 앱의 메모리 상태를 문자열로 정리해 반환합니다.
import android.app.ActivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = findViewById(R.id.text);
textView.setText(getMemoryInfo());
}
private String getMemoryInfo() {
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(memoryInfo);
Runtime runtime = Runtime.getRuntime();
StringBuilder builder = new StringBuilder();
builder.append("Available Memory: ").append(memoryInfo.availMem).append("\n").
append("Total Memory: ").append(memoryInfo.totalMem).append("\n").
append("Runtime Maximum Memory: ").append(runtime.maxMemory()).append("\n").
append("Runtime Total Memory: ").append(runtime.totalMemory()).append("\n").
append("Runtime Free Memory: ").append(runtime.freeMemory()).append("\n");
return builder.toString();
}
}
출력되는 메모리 정보 항목 살펴보기
- Available Memory: 시스템 전체에서 현재 사용 가능한 메모리 양입니다.
- Total Memory: 기기에 탑재된 전체 물리적 메모리(RAM) 크기입니다.
- Runtime Maximum Memory: 해당 앱 프로세스가 사용할 수 있는 최대 힙(Heap) 메모리 한도입니다.
- Runtime Total Memory: 현재 JVM(ART)이 앱에 할당한 총 메모리 크기입니다.
- Runtime Free Memory: 할당된 메모리 중 아직 사용되지 않고 남아 있는 여유분입니다.
참고: 위 예제는 구버전 서포트 라이브러리(android.support.v7.app.AppCompatActivity)를 사용합니다. 최신 Android Studio 프로젝트라면 AndroidX 라이브러리인 androidx.appcompat.app.AppCompatActivity로 임포트 문을 변경하면 됩니다.
4단계: 매니페스트 설정
androidManifest.xml에 다음 코드를 추가합니다. 별도의 권한 없이 메모리 정보를 조회할 수 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 실행(Run) 아이콘을 클릭하고, 목록에서 본인의 모바일 기기를 선택하세요. 기기 화면에 아래와 같이 메모리 정보가 표시됩니다.
