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
1
Question by toborific · Oct 04, 2013 at 07:29 AM · memory leak

Material Memory Leak

I've found that working with renderer.material in some cases will cause a memory leak. It will duplicate materials and fail to remove them, even if the parent object is destroyed. This leads to an ever increasing material count. I found the thread below that discusses some ways to handle this, but I'm not sure how best to apply this info.

http://answers.unity3d.com/questions/192561/renderermaterials-leaking-materials.html

Resources.UnloadUnusedAssets will to get rid of the cloned materials, but I'm not sure where or when to call it. It seems to slow the game down if I call it too often. Has anyone run into this issue and how did you handle it?

Here's some code that will produce a material leak.

using UnityEngine; using System.Collections;

public class RayScript : MonoBehaviour {

 public Color color1 = Color.red;
 public Color color2 = Color.blue;
 public float duration = 3.0f;

 // Use this for initialization
 void Start () {
     renderer.material.SetColor("_TintColor", color1);
             Destroy(gameObject, duration);
 }
 
 // Update is called once per frame
 void Update () {
     
     float lerper = Mathf.PingPong(Time.time, duration) / duration;
     Color newColor = Color.Lerp(color1, color2, lerper);
     renderer.material.SetColor("_TintColor", newColor);

 
 }

}

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
4
Best Answer

Answer by MartinCA · Oct 04, 2013 at 09:03 AM

What you're experiencing is intended behavior. Every call to renderer.material will create a new material instance bound to that renderer. That is so you can modify material attributes without affecting the attributes on other renderers using the same material.

You can clean up the materials explicitly using Resources.Unload( renderer.material ) when done, instead of using Resources.UnloadUnusedAssets, which should be faster.

Either way loading and unloading resources is a relatively expensive operation.

If in your use case you can share attributes between renderers, you can use renderer.sharedMaterial, which will not create new instances but rather update the attributes on the material shared by all renderers.

Comment
Add comment · Show 3 · 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 toborific · Oct 04, 2013 at 03:27 PM 1
Share

Thanks for the great info. Using renderer.shared$$anonymous$$aterial did indeed solve the issue I was seeing. In my use case, effect is slightly different as now all the lines lerp simultaneously rather than on their own individual timers, but I think I understand why this is now.

Also, I found the following: http://docs.unity3d.com/Documentation/ScriptReference/Renderer-material.html

By adding this code to my use case, I was able to specifically destroy the cloned material:

void OnDestroy() { DestroyImmediate(renderer.material); }

avatar image Sisso toborific · Apr 12, 2014 at 02:40 PM 0
Share

@toborifc, please accept this answer so this question could be closed.

avatar image playent · Oct 23, 2017 at 12:33 AM 0
Share

Thanks! We had a similar problem in $$anonymous$$asters of Conquest what cause our server memory to be completely used up. This fix did the trick.

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

18 People are following this question.

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

Related Questions

Creating a MemoryStream from WWW.bytes leaks memory 1 Answer

Unity WebGL build memory discrepancy in local hosted and web hosted 0 Answers

Prefab instantiation and destruction 3 Answers

Unity in embedded QWidget memory problem 0 Answers

Unity full memory crash, during AssetDatabase.LoadAssetAtPath 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