Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Luuk- · Oct 15, 2015 at 01:47 PM · unity 5monoramheap

Mono heap size problem (debugging is hard, no SGen)

Hey all,

For a project at the University, I'm working with a team on a Unity game with high RAM usage. An external company has delivered us a lot of 3D data from the real world. This is data like the data in Google Streetview (as 3D triangle meshes + textures). This data is about ~100MB per 50x50m block, but could increase to ~200-300MB per block.

Seeing how this data is so large, we stream this data via a server. If the player leaves the neighbourhood, the block will be destroyed.

Here comes our problem: Mono keeps the giant heap (which can grow to 6GB) reserved, and does not defragment/compact the heap. The Unity profiler only shows us Unity RAM, which is not really a problem. The Mono RAM however, is not accessible in Unity.

We have multiple questions:

  1. Does anyone know if we can see/profile our Mono RAM somewhere?

  2. Seeing how Unity 5.2.0 uses Mono 2.0, an old garbage collector is used (sadly no SGen). Is there some way to use it or optimize our memory in another way?

If I didn't provide enough info for you guys to answer our questions, I'm sorry! Feel free to ask more!

Thanks in advance.

Luuk-

Comment
Add comment · Show 4
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 _Beyond_ · Oct 20, 2015 at 01:33 PM 1
Share

similar problems here...

avatar image Aranda · Dec 14, 2015 at 02:11 AM 0
Share

Same for us too. We managed to get the heap size under control by manually calling GC.Collect(), but this is far from ideal. We need a way to debug the heap, like CLRProfiler for .Net.

avatar image Bunny83 Aranda · Dec 14, 2015 at 08:13 AM 0
Share

I've posted an answer. A lot people seem to think that Unity is a managed application, but it's not. Unity is mainly written in C++ and Unity does it's own memory management of the native resources. On top of that you have $$anonymous$$ono as scripting environment. $$anonymous$$ono has it's own heap / heap area within the native application.

avatar image Aranda Bunny83 · Dec 14, 2015 at 08:42 AM 0
Share

Thanks for the update, and detailed answer below. It might even be the root of our problem.

1 Reply

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

Answer by Bunny83 · Dec 14, 2015 at 08:09 AM

Well, i guess you might have forgotten to actually destroy the mesh object unless you actually reuse it. Keep in mind that using the .mesh property will create a duplicate of the original mesh. The same thing happens with .material. If you want to completely destroy an object you have to destroy the mesh and material as well.

Also keep in mind that a lot built-in Unity classes are actually "managed proxy classes" for C++ objects. For example:

 void FooBar()
 {
     Mesh m = new Mesh();
 }

calling "FooBar" will create a new Mesh object. The local variable m seems to be the only managed reference to that object and when FooBar finishes you might think that the mesh is up for garbage collection. However that's not the case. Every class that is derived from UnityEngine.Object is a "tracked" object that has a native counterpart in the C++ world. See the class tree for a complete list (just find the "Object" class and see all the derived classes). Even you lost all references to an object you can simply ask Unity for the reference by using FindObjectsOfType. This method can find any UnityEngine.Object derived instance. Likewise all those objects can and have to be destroyed using the Destroy method.

When you call Resources.UnloadUnusedAssets, Unity will destroy all objects which aren't currently in use. Our mesh object would be destroyed as well. UnloadUnusedAssets is implicitly called when you load a new scene. So objects that might have created materials, meshes, audioclips, textures, shaders, ... usually don't have to worry about cleaning up those objects when a scene change happens. However if you constantly creating new objects without cleaning up the hidden siblings, you just created a memory leak.

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 Aranda · Dec 14, 2015 at 02:24 PM 0
Share

Thanks, this is a great answer and advances my understanding of the internals of Unity. So calling the Destroy() method is akin to the .Net Dispose()? In our case something is chewing up native resources and calling GC.Collect() releases them. This implies that when the the GC gets to the offending managed object, it is either doing Dispose or Destroy, but since we've been careful with managed allocations, the GC doesn't kick in automatically.

So my question is, if we don't load a new scene or call UnloadUnusedAssets, must we explicitly call Destroy or could the GC be destroying those UnityEngine.Object instances for us?

avatar image Aranda · Dec 14, 2015 at 02:25 PM 0
Share

Sorry, one more question without notice :)

You said setting .mesh creates a copy, so setting .shared$$anonymous$$esh avoids that right?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,Can I get runtime data of unity without needing source code? 0 Answers

What is `assets/bin/Data/Managed/etc/mono/xxx` contain ? 0 Answers

mono.security.dll is denied? 1 Answer

Why C# 6.0 features could be used in manual plugin (dll) but not able to compile when used as Unity Script directly? 0 Answers

Out of memory error 0 Answers


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