I am currently developing a 2D sprite-based game (using SpriteManager) for Unity iOS using C#. On the iPhone, I am noticing horrible performance periodically (perhaps every 30-60 seconds). After some digging and using the internal iOS profiler, I noticed that my “used heap” size is reportedly increasing every time the profiler reports new data.
This used heap size grows until it approaches the “allocated heap” size, at which point one of two things happens:
-
The (I assume) OS allows the allocated heap to grow, allowing for the used heap to continue to grow. This only delays the inevitable garbage collection.
-
Most commonly, the garbage collector is called to cleanup memory, reducing the use heap size.
The issue I am having is that when the garbage collector is called, the “used heap” size properly reduces in size, but the time required for the garbage collector to complete its task is sometimes reported to take longer than 80ms. This, of course, is causing the game to freeze for several seconds while the garbage collector runs.
This is my first iOS app and I have a few questions regarding the heap:
-
Is it normal for the heap to steadily grow this way? If I am managing my memory correctly, shouldn’t I be seeing the heap rise and fall, rather than steadily increase? I use a pooling system so that all my objects needed for a game level are instantiated at the beginning, so nothing is being Instiated() or Destroyed() as the game progresses.
-
Is it inevitable that the garbage collector will eventually need to free up memory, even with proper memory management? Will my used heap size eventually reach the allocated heap if I don’t call the garbage collector manually?
–
SOLVED:
My particular issue had more to do with memory management rather than the garbage collector. For those having issues with large garbage collection times, I would encourage looking into the following:
For iOS, Use the internal profiler to track your used heap size and when the garbage collector calls. http://unity3d.com/support/documentation/Manual/iphone-InternalProfiler.html
A used heap that is rising quickly can be indicative of a memory leak. Although it may seem that the garbage collector is causing the issue due to large collection times, it may be due to the amount of used memory and the garbage collector having to analyze a large amount of memory to see what can be recovered. Manually calling the garbage collector is an option and may work for you, but always use profiling tools before and after manual calls to the GC to determine if it is beneficial.