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
10
Question by Daniel Sperry 2 · Jun 10, 2010 at 06:40 AM · monomemory-leakbestpractices

Are there any known memory leaks in unity3d or mono?

Is there any mono or unity3d api call or language construct to be avoided to prevent memory leaks?

I am not talking about programming errors from us, unity3d users, like keeping references to lots of data thus preventing the garbage collection to work.

I mean the kind of stuff that is legal and should work just fine but due to known implementation bugs we must avoid.

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

2 Replies

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

Answer by Ashkan_gc · Jun 13, 2010 at 12:02 PM

there are not such api known but there are practices that you can do to make garbage collection faster and better.

  1. don't allocate memory (specially for large objects) in functions like Update because this will cause multiple allocation and deallocation.
  2. unity/mono's garbage collector is not an advanced one like what you have in .NET or java. it don't use generation based collection and different heap parts for different size objects. i don't know how much it improved for mono 2.6 but it not as good as .NET's GC.
  3. use GC.Collect() whenever you think you have time and allocated many objects that you don't use any more. why you should wait for the garbage collector.
  4. if you use unmanaged (native) plugins try to implement appropreate interfaces to unload resources properly.

memory for objects like meshes and textures will not be collected when you call GC.Collect(0); because they are not managed data and the managed apis just create them in unmanaged memory for you. if you want to know more about memory leaks that unity had before and solved. take a look at this page. i think there was not any. the mono version is too old but in 3.0 we will have mono 2.6 wich is a big improvement.

Comment
Add comment · Show 2 · 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 Mike 3 · Jun 13, 2010 at 06:32 PM 1
Share

Good read up besides #3. GC.Collect should only ever be called as GC.Collect(0), especially in $$anonymous$$ono. On top of that, Texture2D memory is 99% in native memory, so garbage collection basically just reclaims a single, almost empty class

avatar image Ashkan_gc · Jun 14, 2010 at 04:06 AM 0
Share

thank you mike. I'll edit it to correct my mistake about Texture2d but when i wrote GC.Collect() i just meant GC.Collect without any specification of empty parameter list or an special argument. i can not find any reason for GC.Collect(0) because mono don't use generations as i know. how can i remove the leaks caused by Texture2ds?

avatar image
1

Answer by piojo_work · Aug 22, 2011 at 08:10 AM

I have found unity memory leaks. One seems to be any creation of Texture2D objects. I worked around this by only creating them once, and using Resize() and SetPixels() after that.

I am also seeing memory that's never reclaimed in a function that modifies a large Color[] array, looping over it and manipulates each of values before it is set as the pixels of a Texture2D object (persistent).

I made a testcase and filed a bug today--in our actual game, the memory grows without bound. In the testcase, the Unity editor's memory grows without bound, but the standalone build gets to 435M and does not increase beyond that. Changing scenes definitely does not free the memory, because that is part of the test. The problem is more serious when the work is done in a separate thread.

UPDATE: The problem occurs whenever a temporary thread accesses non-static data members or functions. When this happens, all of the class's memory fails to be released when the class is unloaded in a scene change.

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 theschwartz · Mar 29, 2012 at 02:28 PM 0
Share

I am also experiencing memory leaks as well. Could you explain your update a bit more in detail? Does it include any type of C# classes or is it just Unity C# classes? The game my company is developing uses extra threads beside from the Unity main thread, so I am wondering if this can have something to do with it?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Crashing in build version NOT in Editor 0 Answers

Trouble Downloading Unity PDB Symbols from Symbol Server 0 Answers

Physics.Raycast not working on il2cpp 0 Answers

How do I release editor memory? 2 Answers

editor script memory leak 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