unitycoder / UnityMobilePaint

Public repository for MobilePaint issues & requests
https://www.assetstore.unity3d.com/en/#!/content/19803?aid=1101lGti
MIT License
49 stars 16 forks source link

Memory allocation not released #10

Closed unitycoder closed 8 years ago

unitycoder commented 8 years ago

I have run into a problem. Whenever I call upon the paint scene it increases the memory with 2.5-2.6 MB! This increase NEVER disappears, it only happens once, but it’s still annoying. I tried running System.GC.Collect() and Resources.UnloadUnusedAssets(). So what is causing this increase and how do I dispose of it?

unitycoder commented 8 years ago

Can you post screenshot of profiler / memory sample window? (does it happen in editor only?)

martinmaxk commented 8 years ago

Don't know why you want screenshots, the only reason I can think of is that you don't trust me, but here they are:

The focus point is the text at the top all the way to the right it says (x MB) that's the memory dump.

Before:

skaermbillede 2015-12-09 14 59 34

After: skaermbillede 2015-12-09 14 59 43

Profiler: skaermbillede 2015-12-09 14 59 46

martinmaxk commented 8 years ago

Oh yeah by the way forgot to include the memory part of the profiler.

skaermbillede 2015-12-09 15 49 36

martinmaxk commented 8 years ago

I can also confirm that the increase has something to do with the MobilePaint script.

martinmaxk commented 8 years ago

I figured it out. Whenever you create a new byte array the memory heap goes up. But how to deallocate that memory again, hmm...

martinmaxk commented 8 years ago

The GC should take care of the byte arrays when the scene is destroyed and the value of the byte arrays are null, but I guess for some reason they linger on between scenes, maybe there's a reference to them in an object that isn't deleted in between?

martinmaxk commented 8 years ago

Discovered how to fix this some time ago. I bet it could be done more effectively (iterate over all public and private byte[] variables), but this works for me. Just assign the byte arrays to null and you're good to go. You don't have to explicitly call the GC, but I did it anyways to make it more instant. Place it in MobilePaint script and when the object is deleted, it's cleaned too.

I'm only using these two byte arrays. BTW if you find out how to do this more effectively like I described, could you send me the code? Thanks.

void OnDestroy() { undoPixels = null; pixels = null; System.GC.Collect(); Debug.Log(System.GC.GetTotalMemory(false)); }

unitycoder commented 8 years ago

nice, that solution seems fine, no other ideas right now.

unity gc> http://docs.unity3d.com/Manual/UnderstandingAutomaticMemoryManagement.html

unitycoder commented 8 years ago

added OnDestroy() cleaning up to v1.96