Garbage collection during title screen

When my game title screen is up i notice that my used heap value is always increasing and the garbage collection time duration often seems to be pretty high (17)

As far as i can see from my scripts i’m not doing allocations in my main loops. Everything is a member variable (using C#)

All i’m doing on my title screen is a few GUI elements. Extremely simple stuff!

Any ideas how i can cut down that garbage collection time?

Hello,
maybe you are playing with strings? Please post some sample code…

No string allocations as far as i can see.

If no allocations are being done, should the used heap value be static and garbage collection time be zero?

If there are no allocations then heap usage remains constant and garbage collection doesn’t happen.

Not all the allocations are explicit, some could be caused by various standard library components, like string concatenation with “+” operator, use of iterators or use of coroutines. It is hard to advice something without looking at the code.

I created a stripped down project. All i have is a camera (the standard one created with a new project) and a game object with the code below for a script. I see the used heap changing all the time and collection duration anywhere between 0.0 → 12.0

Game object script code is;

using UnityEngine;

public class Test : MonoBehaviour {

	private enum TestMode : int {
		Mode1,
		Mode2,
		Mode3,
	}
	
	private TestMode mMode;
	
	void Start () {
		mMode = TestMode.Mode1;
	}
	
	void Update () {
		switch (mMode) {
				case TestMode.Mode1:
					break;
				case TestMode.Mode2:
					break;
				case TestMode.Mode3:
					break;
		}
	}
	
	void OnGUI() {
	}
	
}

iPhone Unity internal profiler stats:
cpu-player> min: 6.1 max: 14.2 avg: 8.3
cpu-ogles-drv> min: 0.0 max: 0.0 avg: 0.0
cpu-present> min: 1.3 max: 3.4 avg: 1.5
frametime> min: 31.4 max: 35.6 avg: 33.5
draw-call #> min: 0 max: 0 avg: 0 | batched: 0
tris #> min: 0 max: 0 avg: 0 | batched: 0
verts #> min: 0 max: 0 avg: 0 | batched: 0
player-detail> physx: 0.8 animation: 0.0 culling 0.2 skinning: 0.0 batching: 0.0 render: 6.4 fixed-update-count: 1 .. 2
mono-scripts> update: 0.2 fixedUpdate: 0.0 coroutines: 0.0
mono-memory> used heap: 200704 allocated heap: 266240 max number of collections: 1 collection total duration: 7.6

Hmmm, seems that if i comment out the OnGUI function i don’t see any garbage collection activity even though nothing is being done in that function.

I’ve found that just by having a single OnGUI function somewhere that does nothing dropped my frame rate in half in some cases. So it definitely does something, even if it’s empty.

OK, so it looks like OnGUI is the culprit here. Removing it from my game stops the garbage collection problems but it’s obviously not a solution as i have a fair bit of UI stuff in my game.

onGui is expensive on the iphone, says so in the docks. Don’t use it, ever.

I can probably create my own GUI using a textured plane attached to my camera, but i also use a freetype font via GUI.skin.font i’m not sure how i could get that font into the game without the Unity GUI

GUIText lets you attach fonts.

Yes but i would still have to use OnGUI and that’s what is causing the garbage collection on the iPhone

You attach GUIText objects to any game object. You don’t have to use OnGUI to use GUIText components.

IC …i will give that a try. Thanks.

So for anyone following along with this who has similar issues, my garbage collection problems were solved by removing all OnGUI functions from my game and instead attaching GUITexture and GUIText components to my game objects.

Thanks to all for help.

That’s a little disappointing. I understand that OnGUI is slow. I was just hoping to use if for sections of the game that aren’t really time critical. For example, I have objects that the user can select and then I display a menu that lists the objects that that item contains.

At that point, I stop the game time and cover the screen with the Inventory screen built with OnGui. It seems to work fine on all the devices I’ve tried it on. I haven’t look at garbage collection yet but maybe I should be for I get too attached to OnGUI.

I understand that I can build a gui using SpriteManager and such but the Unity GUI does has some nice features like scroll views that I really don’t care to code.

The garbage collection CPU spikes might not be a problem in your game if your user is just focusing on using the UI and not interacting with the game world. In my game the GUI is an overlay on the main 3D view so i was seeing performance glitches in my frame rate while animations were playing etc due to the garbage spikes.

There are places that I use it. I don’t follow the mantra of “never use OnGUI”. I just using in places that are appropriate (like when my game actually isn’t running…just the main menus for example). Programming using the GUI API is extremely convenient compared to the extremely clunky GUIManager (which I do use, but I had to modify the crap out of it in order for it to be more robust and useful…and I’m still constantly modifying it to make it more robust and useful).