Unity 메모리 프로파일러 모듈 가이드
이 문서는 Unity 에디터에서 메모리 사용량을 분석하는 데 도움을 주기 위한 공식 가이드입니다. 메모리 프로파일링을 통해 애플리케이션의 메모리 사용 패턴을 이해하고, 문제를 해결하는 데 유용한 정보를 제공받을 수 있습니다.
메모리 프로파일러 모듈 개요
Unity에서는 애플리케이션의 메모리 사용량을 분석하는 두 가지 주요 방법을 제공합니다:
- 메모리 프로파일러 모듈: 기본적으로 제공되는 프로파일러 모듈로, 애플리케이션의 메모리 사용 정보를 시각화합니다.
- 메모리 프로파일러 패키지: 프로젝트에 추가하여 사용할 수 있는 패키지로, 더 자세한 메모리 분석이 가능합니다.
메모리 프로파일러 모듈의 기능
메모리 프로파일러 모듈은 애플리케이션에서 사용된 총 메모리 양을 시각적으로 표시합니다. 주요 기능은 다음과 같습니다:
- 로드된 오브젝트의 수와 카테고리별 메모리 사용량 보기
- 각 프레임당 가비지 컬렉션(GC) 할당 수 확인
에디터에서의 메모리 프로파일링
Unity 에디터에서 애플리케이션을 프로파일링할 경우, 배포된 애플리케이션보다 더 높은 메모리 사용량이 보고되는 경우가 있습니다. 이는 에디터가 추가적인 오브젝트를 사용하고 메모리를 차지하기 때문입니다. 따라서 타겟 플랫폼에서 실행 중인 빌드 버전을 프로파일링하는 것이 좋습니다.
차트 카테고리
메모리 프로파일러 모듈은 여러 카테고리로 나뉘어 메모리 사용 정보를 제공합니다.
카테고리 | 설명 |
---|---|
Total Allocated | 애플리케이션이 사용한 총 메모리 양입니다. |
Texture Memory | 애플리케이션의 텍스처가 사용한 총 메모리 양입니다. |
Mesh Memory | 애플리케이션의 메시가 사용한 총 메모리 양입니다. |
Material Count | 애플리케이션의 머티리얼 인스턴스 수입니다. |
Object Count | 애플리케이션의 네이티브 오브젝트 인스턴스 수입니다. |
GC Used Memory | GC 힙이 사용한 메모리 양입니다. |
GC Allocated in Frame | GC 힙에서 프레임당 할당된 메모리 양입니다. |
모듈 세부 정보 창
모듈 세부 정보 창에는 두 가지 뷰가 있습니다:
- Simple: 프레임당 메모리 통계의 고수준 개요를 표시합니다.
- Detailed: 메모리 사용에 대한 자세한 정보를 제공합니다.
뷰를 변경하여 원하는 정보를 쉽게 확인할 수 있습니다.
간단한 뷰와 통계
간단한 뷰는 Unity의 메모리 사용을 실시간으로 표시합니다. 주요 프로퍼티는 다음과 같습니다:
프로퍼티 | 설명 |
---|---|
Normalized | 프레임의 메모리 사용량을 조정합니다. |
Total Committed Memory | 전체 메모리 양을 표시합니다. |
System Used Memory | Unity가 사용하는 메모리를 추적합니다. |
GC Used Memory | 가비지 컬렉터에 의해 수집된 메모리 양입니다. |
코드 예제
아래는 메모리 프로파일러 모듈의 카운터에 접근하는 간단한 스크립트입니다. 이 스크립트는 Total Reserved Memory, GC Reserved Memory, System Used Memory를 수집하여 GUI에 표시합니다.
using System.Text;
using Unity.Profiling;
using UnityEngine;
public class MemoryStatsScript : MonoBehaviour
{
string statsText;
ProfilerRecorder totalReservedMemoryRecorder;
ProfilerRecorder gcReservedMemoryRecorder;
ProfilerRecorder systemUsedMemoryRecorder;
void OnEnable()
{
totalReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory");
gcReservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory");
systemUsedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory");
}
void OnDisable()
{
totalReservedMemoryRecorder.Dispose();
gcReservedMemoryRecorder.Dispose();
systemUsedMemoryRecorder.Dispose();
}
void Update()
{
var sb = new StringBuilder(500);
if (totalReservedMemoryRecorder.Valid)
sb.AppendLine($"Total Reserved Memory: {totalReservedMemoryRecorder.LastValue}");
if (gcReservedMemoryRecorder.Valid)
sb.AppendLine($"GC Reserved Memory: {gcReservedMemoryRecorder.LastValue}");
if (systemUsedMemoryRecorder.Valid)
sb.AppendLine($"System Used Memory: {systemUsedMemoryRecorder.LastValue}");
statsText = sb.ToString();
}
void OnGUI()
{
GUI.TextArea(new Rect(10, 30, 250, 50), statsText);
}
}
결론
Unity 메모리 프로파일러 모듈은 애플리케이션의 메모리 사용에 대한 자세한 정보를 제공하여 메모리 문제를 파악하고 해결하는 데 도움을 줍니다. 프로파일러를 사용하여 로드된 오브젝트와 메모리 할당량을 분석함으로써 최적화할 수 있는 영역을 찾을 수 있습니다.