- Home /
Best practices for garbage collection?
I've noticed the longer my app runs the more memory it has allocated (judging by the Task Manager anyway). So time to look for leaks.
Wondering what are best practices for memory management?
Is it safe to assume that things that go out of scope will clean up? I use the WWW object to get xml data and load remote pictures (from a video camera) quite often, do I need to worry about those bits? Do I need to destroy a texture, for example, before I load a new one, or vice versa?
Any tips welcome.
Answer by Eric5h5 · Sep 10, 2010 at 11:52 PM
Unity objects like textures are not garbage collected and need to be manually destroyed. Currently with Unity 2.6, there's no way to actually free the memory aside from loading a new level. Unity iPhone has Application.GarbageCollectUnusedAssets, which can be called at any time. I believe this functionality is available across the board in Unity 3, under a different name.
Mono objects and variables are handled automatically by Mono's garbage collection.
Ok, so what would the sequence be if, say, I'm constantly re-loading a texture with www.LoadImageIntoTexture(tex) ? If I've done 'www = new WWW(url...)' do I need to destroy 'www'? Do I somehow clean 'tex' before loading an image from www onto it?
@DaveA: LoadImageIntoTexture is the best way for reloading textures; as the docs say, "If you continously download textures you must use LoadImageIntoTexture or Destroy the previously created texture."
Thanks. What about a WWW object? I new one up, expect it to go out of scope and die, but should I Destroy it first?
This is interesting.
When you modify a Renderer's shared$$anonymous$$aterial, it implicitly creates a new $$anonymous$$aterial object.
Does this mean that I should be manually destroying these? Or other objects created similarly? Or will destroying the renderer component to which they are attached free them?
Answer by jajskadu · Nov 01, 2012 at 05:51 AM
The most basic collection algorithm "mark - Clear all objects that need to be recycled (Mark-Sweep) algorithm, such as its name, the algorithm is divided into the" mark "and" clear "in two stages: First, mark, mark completedafter unification recycling out all objects are marked, mark fact, the first one about the object marker determine basic introduction over. We say it is the most basic collection algorithm, and the subsequent collection algorithms are based on this line of thinking, and to improve its shortcomings. Its main disadvantage of the two: a question of efficiency, marking and clearance process efficiency is not high; another space, space debris will produce a large number of non-contiguous memory fragmentation, too much may cause the flag is cleared, when can not find enough contiguous memory in the program need to allocate a large object after running had to trigger another garbage collection action in advance.
This doesn't really make sense, could you rewrite your answer?
Answer by Edy · Sep 10, 2010 at 11:14 PM
Try the advices here:
http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx
and here:
http://msdn.microsoft.com/en-us/library/ms973837.aspx
Same principles apply in Unity.
I've read elsewhere that Unity items are handled differently