오브젝트 풀링 기법

 [C#] 가비지 컬렉션과 오브젝트 풀링 : 네이버 블로그 (naver.com)


아래 클래스는 오브젝트 풀링을 일반화하여 구현한 내용이다. (ObjectPool<T>, PooledObject<T>)

public class ObjectPool<T> { private List<PooledObject<T>> _pooledObjects; public delegate T RequestObjectDelegate(); private RequestObjectDelegate _requestObjectCallback; public ObjectPool(RequestObjectDelegate requestObjectCallback) { _pooledObjects = new List<PooledObject<T>>(); _requestObjectCallback = requestObjectCallback; } public PooledObject<T> GetObject() { PooledObject<T> pooledObject = SelectPooledObject(); if (pooledObject == null) pooledObject = AddPooledObject(); pooledObject.active = true; return pooledObject; } private PooledObject<T> SelectPooledObject() { int count = _pooledObjects.Count; for (int index = 0; index < count; index++) { PooledObject<T> pooledObject = _pooledObjects[index]; if (!pooledObject.active) return pooledObject; } return null; } private PooledObject<T> AddPooledObject() { PooledObject<T> newPooledObject = new PooledObject<T>(this, _requestObjectCallback()); _pooledObjects.Add(newPooledObject); return newPooledObject; } }
public class PooledObject<T> { private ObjectPool<T> _objectPool; private bool _active; private T _value; public ObjectPool<T> objectPool { get { return _objectPool; } } public bool active { get { return _active; } set { _active = value; } } public T value { get { return _value; } } public PooledObject(ObjectPool<T> objectPool, T value) { _objectPool = objectPool; _active = false; _value = value; } }

아래는 ObjectPool<T> 클래스를 활용한 예시이다.

public class Example { private ObjectPool<ExampleObject> _objectPool; private List<PooledObject<ExampleObject>> _objects; public Example() { _objectPool = new ObjectPool<ExampleObject>(CreateObject); _objects = new List<PooledObject<ExampleObject>>(); } private ExampleObject CreateObject() { return new ExampleObject(); } public void AddObject(string content) { PooledObject<ExampleObject> addObject = _objectPool.GetObject(); addObject.value.Initialize(content); _objects.Add(addObject); } public void RemoveObject(int index) { try { PooledObject<ExampleObject> removeObject = _objects[index]; removeObject.active = false; _objects.RemoveAt(index); } catch (IndexOutOfRangeException) { Console.WriteLine("Index out of range"); } } public void PrintObjects() { int count = _objects.Count; for (int index = 0; index < count; index++) Console.WriteLine(_objects[index].value.ToString()); } } public class ExampleObject { private string _content; private DateTime _createdTime; public ExampleObject() { _createdTime = DateTime.Now; } public void Initialize(string content) { _content = content; } public override string ToString() { return string.Concat(_content, " (", _createdTime.ToString(), ")"); } }
class Program { static void Main(string[] args) { Example example = new Example(); string buffer = string.Empty; do { Console.Write("> "); buffer = Console.ReadLine(); switch (buffer) { case "add": Console.Write("Content : "); example.AddObject(Console.ReadLine()); break; case "remove": Console.Write("Index : "); example.RemoveObject(int.Parse(Console.ReadLine())); break; case "print": example.PrintObjects(); break; } } while (buffer != "exit"); } }

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

Shader informations nice blog ~ ~