Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by DaveA · Sep 10, 2010 at 09:16 PM · memorygarbage-collectionmanagementleakcollection

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Sep 11, 2010 at 12:01 AM 0
Share

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?

avatar image Eric5h5 · Sep 11, 2010 at 12:53 AM 0
Share

@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."

avatar image DaveA · Sep 11, 2010 at 01:20 AM 0
Share

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?

avatar image Eric5h5 · Sep 11, 2010 at 01:45 AM 0
Share

@DaveA: you can't destroy a WWW object.

avatar image rhys_vdw · Nov 01, 2012 at 12:08 AM 1
Share

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?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhys_vdw · Nov 01, 2012 at 05:58 AM 0
Share

This doesn't really make sense, could you rewrite your answer?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Sep 10, 2010 at 11:57 PM 0
Share

I've read elsewhere that Unity items are handled differently

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Possible material or mesh leak 1 Answer

Don't allocate memory in Update loops on iOS. - How strictly should this be followed? 2 Answers

Reasonable heap alloc. per second and total ? 0 Answers

Memory Issues in Unity for Android and IOS 1 Answer

Unity memory leak when using a drawing tablet? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges