Many a times while developing a game in unity3D we encounter issues like crashing in between the game play due to low memmory warnings. This is because the game is consuming unexpectedly high memmory. Developers not usually think of this while developing and encounter a head boggling issue of Game Crashing while testing.
This Blog might help you to take some key points in mind while developing the Game.
Common causes of unnecessary heap allocation:
Avoid foreach loops:
Avoid foreach loops and use for or while loops instead.The reasoning behind this is compiler will preprocess code such as this:
foreach (SomeType s in someList)
s.DoSomething();
...into something like the the following:
using (SomeType.Enumerator enumerator = this.someList.GetEnumerator())
{
while (enumerator.MoveNext())
{
SomeType s = (SomeType)enumerator.Current;
s.DoSomething();
}
}
In other words, each use of foreach creates an enumerator object - an instance of the System.Collections.IEnumerator interface - behind the scenes.So, Don't use them in C# code that you allow Unity to compile for you. Do use them to iterate over the standard generic collections (List etc.) in C# code that you compile yourself with a recent compiler.
Coroutines
If you launch a coroutine via StartCoroutine(), you implicitly allocate both an instance of Unity's Coroutine class and an Enumerator . Importantly, no allocation occurs when the coroutine yield's or resumes, so all you have to do to avoid a memory leak is to limit calls to StartCoroutine() while the game is running.
Limit Variables
Use as less as possible public variables as they take up memmory and remains there no matter they are being used or not. If needed use Resources.UnloadUnusedAssets() that will free up memmory from all the unused assets that might consume memmory.
Asset Bundles
If you are using asset bundles, you should not in any time frame forget to dispose www as it will otherwise be a pain. For that you can use: www.Dispose();
These are only few of the points from a long list that I will be sharing in my other blogs. Hope this will help you in many ways.
Happy Coding..................
0 Comment(s)