0
\$\begingroup\$

I'm experiencing a memory leak in my Unity game. If the game sits idly (no new game objects are being created in the scene), Windows Task Manager will show the memory of the .exe slowly rising.

I've attached the Unity profiler and the memory profiler stays completely flat with no new objects being created and its reported memory numbers don't rise. Thus, I assumed it must be from garbage allocations each frame somewhere. So I check the CPU profiler and there were a few scripts that were showing a few bytes of GC Alloc, most of which I've fixed. However, the memory leak appears to still be present.

I've gone as far as systematically removing objects from the scene to see if the memory leak stops, but that hasn't proven fruitful.

What's the next step to take to track this down? What other tools can I use?

I'm also noticing that the amount of memory being released between scene changes isn't great (very little in fact, even after requesting a GC collect). I'm hoping this is related to the memory leak during the scene though.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Are you creating new strings on each frame? Like FPS value or something like that? \$\endgroup\$ Nov 20, 2014 at 13:22
  • \$\begingroup\$ I was in a few instances (as identified in the Unity CPU profiler), which I've fixed. It's possible there are places where I may be inadvertently still. I guess my counter question would be, besides thumbing through all of my scripts, what's a good tool to check that? From what I see in the Unity CPU profiler, by checking the GC Alloc column, there's no String garbage being allocated. \$\endgroup\$ Nov 20, 2014 at 13:27
  • \$\begingroup\$ does it level off? in GC environments collections usually only happen when needed and memory released by GC may still be reserved as far as the OS is concerned (it sees it as in use). \$\endgroup\$ Nov 20, 2014 at 13:40
  • \$\begingroup\$ It doesn't seem to level off. I've let it run idly for 30+ minutes and the memory continues to rise, slowly, but surely. \$\endgroup\$ Nov 20, 2014 at 14:00

1 Answer 1

1
\$\begingroup\$

Memory leaks in Unity can occur on several places. But to help you in the right direction, one of the most common leaks in unity would be the

  • Texture/ material leaks
  • Imperfect loops (for, foreach, while, etc)

These leaks are often hard to track and can lead to crashes. To properly chase them down you first want to make sure that you have no warning message, and no errors. Make sure that warnings are not ignored using #pragma's and the likes, and run some form of code analysis over your codes.

After making sure your code has no errors/warnings you will want to check your textures and materials. They fairly often lead to leaks, in several different ways such as Renderer.materials. Static references to materials/textures can also lead to leaks, and the same counts for singletons if not properly cleaned up after.

If even after this information you are still unsure to where the leaks came from, you will either need to provide some source, or check out the #unity3d IRC. which has quite the helpful community (and off course if you find the answer there, don't forget to post it here as well for other people to find)

\$\endgroup\$
3
  • \$\begingroup\$ Regarding texture/material leaks, wouldn't that show up in the Unity memory profiler? \$\endgroup\$ Nov 20, 2014 at 15:14
  • \$\begingroup\$ Neeko, yes and no. I recently had one myself profiler which did not seem to show up, but caused a crash in both unity editor and standalones alike. crash screen trying to run with ~ 12gb of memory \$\endgroup\$
    – Dimitri mx
    Nov 20, 2014 at 15:16
  • \$\begingroup\$ Okay, just wanted to verify how accurate the profiler can be. I've also seen the profiler, when running within the editor, show no GC alloc for a particular script method, but have it show GC alloc when attached to a standalone client. Thanks for the info! \$\endgroup\$ Nov 20, 2014 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .