08
01

가비지 컬렉션 스파이크

스파이크란 Unity에서 가비지 컬렉터가 할당된 메모리 양이 급격하게 증가하고
그 후에 다시 수집되는 현상을 말한다.
이러한 스파이크는 프레임 속도 저하나 버벅임과 같은 성능 문제를 일으킬 수 있다.

가비지 컬렉션 스파이크가 발생할 수 있는 상황

객체를 자주 생성 및 소멸:
When objects are frequently created and destroyed, it can lead to spikes in garbage collection as the memory used by these objects needs to be reclaimed.
다수의 임시 객체 생성:
If your code generates a large number of temporary objects, such as strings or arrays, without reusing or pooling them, it can result in frequent garbage collection spikes.
비효율적인 데이터 구조 사용:
Inefficient data structures that allocate and deallocate memory frequently, like List<T> or Dictionary<T>, can contribute to garbage collection spikes. Consider using object pooling or more efficient data structures to reduce the number of allocations and deallocations.
업데이트 또는 렌더링 루프에서 메모리 할당:
If memory allocations occur within critical performance loops like Update() or rendering functions, it can lead to spikes in garbage collection, affecting frame rates and causing stuttering.
문자열 조작의 과도한 사용:
String concatenation or manipulation operations, especially within loops, can generate temporary strings and trigger frequent garbage collection. Using StringBuilder or other string manipulation techniques can help mitigate this.
고해상도 타이머 또는 이벤트 기반 시스템:
Systems that rely heavily on high-resolution timers or event-driven architectures may generate frequent temporary objects, leading to increased garbage collection.

가비지 컬렉션 스파이크 완화 방법

오브젝트를 재사용하고 해당되는 경우 오브젝트 풀링을 구현한다.

효율적인 데이터 구조와 알고리즘을 사용한다.

특히 성능이 중요한 섹션 내에서 문자열 조작 작업을 최소화한다.

코드를 프로파일링하고 과도한 메모리 할당이 발생하는 영역을 식별한다.

개체 풀링, 캐싱 또는 효율적인 데이터 구조 사용과 같은 최적화 기술을 구현한다.

 

COMMENT