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
0
Question by Dean_147 · Feb 05, 2015 at 04:56 AM · texturetexture2dmemory leakgarbage collectionout of memory

Texture2D Generation Causing Memory Leak (System Out of Memory)

I have a C# script that will generate a texture and apply it to a mesh. If I run the generation repeatedly, Unity takes up more and more memory and eventually crashes with a System Out Of Memory error.

alt text

Here is the code I have to create the texture, and apply it to the object.

 CurrentTileMapTexture = new Texture2D(textureWidth, textureHeight);
 
 CurrentTileMapTexture.Apply();
 
 var meshRenderer = GetComponent<MeshRenderer>();
 meshRenderer.sharedMaterial.mainTexture = CurrentTileMapTexture;
 

I have tried clearing the memory before creating the texture with this:

 Texture2D.DestroyImmediate(CurrentTileMapTexture, true);
 Texture2D.DestroyImmediate(meshRenderer.sharedMaterial.mainTexture, true);
 
 Resources.UnloadAsset(CurrentTileMapTexture);
 Resources.UnloadAsset(meshRenderer.sharedMaterial.mainTexture);
 
 CurrentTileMapTexture = null;
 meshRenderer.sharedMaterial.mainTexture = null;
 
 GC.Collect();
 

But this didn't prevent the memory leak. (not sure if I'm using these functions correctly though)

I've tried researching online but couldn't find a solution. Any suggestions?

error.png (23.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by andrew-lukasik · Feb 21, 2018 at 01:19 PM

UnityEngine.Object.Destroy should work for memory leaks like that. If not - that might be a engine version-specific bug.

 UnityEngine.Object.Destroy( currentTexture );
 currentTexture = new Texture2D( w , h );

Sidenote: I suspect that every UnityEngine.Object is referenced inside engine code somewhere hence GC.Collect alone won't release them. If that is the case then Destroy is kind of abstraction to (at least) de-reference given object from internal collections.

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 Bunny83 · Feb 21, 2018 at 04:13 PM 2
Share

Correct. Every class that is derived from UnityEngine.Object has a native code counter part. All UnityEngine.Object derived classes are "tracked" on the native side and can be found again using FindObjectsOfType. If you want to get rid of any such object you have to destroy it. This also includes: GameObjects, $$anonymous$$eshes, $$anonymous$$aterials, AudioClips, .... In short any kind of object that can be serialized as asset. Note that when destroying a GameObject Unity does some additional cleanup. So all components on that gameobject are destroyed as well. When the Transform component is destroyed it will also destroy all child objects of that Transform. If a Renderer has instantiated a personal instance of a $$anonymous$$aterial that instance should also be destroyed automatically when the Renderer got destroyed. Textures however are never destroyed automatically.

avatar image Krish-Vikram Bunny83 · Feb 26, 2018 at 01:15 PM 0
Share

What about textures/sprite in animation clip ? does it load all the sprite in memory directly or just loads sprite frame by frame.?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

www.texture memory leak 1 Answer

SetPixels32 called with invalid number if pixels in the array 0 Answers

How to not see white background on texture 1 Answer

Unity changes pixel colors 2 Answers

Texture Compression format for UWP,Texture 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