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 Hermonirr · Jun 11, 2015 at 05:38 AM · listtexture2dmemory

Memory leak in Texture2D.Apply()?

Hello,

I'm creating a texture from a list of bytes which I get from external source. When using a new list each frame, there is a memory leak both in Unity Editor and the runtime app. If I use the same list there is no memory leak.

However, even when I use a new list, if I comment out the line newTexture.Apply(); then there is no memory leak. Which leads me to suspect the Apply() method.

I asked in several forums and did not find a solution.

You can download the minimal project to reproduce the problem here: https://goo.gl/axFJDs

Here is the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class testMemory : MonoBehaviour {
     
     public bool                useNewList = false;
     Texture2D                newTexture;
     public byte             color;
     List<Color>                newColors;
     List<byte>                staticPixelColorsList;
     Color                    tempColor = Color.white;
     
     // Use this for initialization
     void Start () {
         
         newTexture = new Texture2D(0,0);
         newColors = new List<Color>();
         staticPixelColorsList = new List<byte>(120000);
         GetComponent<Renderer>().material.mainTexture = newTexture;
     }
     
     // Update is called once per frame
     void Update () {
         
         // Make sure we're using different colors for every frame
         color = (byte)((Time.time*255) % 255);
         
         if(useNewList)
         {
             // This option causes a memory leak, but it's the one I need 
             // because I'm getting the texture data from an external source
             int newWidth  = Random.Range(200,400);
             int newHeight = Random.Range(200,400);
             
             // Create a new list each time
             List<byte> newPixels = new List<byte>(newWidth * newHeight);
             for(int i = 0; i < newWidth * newHeight; i++)
             {
                 newPixels.Add ((byte)(color * i / 120000f));
             }
             setTexture(newPixels, newWidth, newHeight);
             
             // Clear the list (yeah, right)
             newPixels.Clear ();
         }
         else
         {
             // Use the same list, but assign new colors
             for(int i = 0; i < 120000; i++)
             {
                 staticPixelColorsList.Add ((byte)(color * i / 120000f));
             }
             setTexture(staticPixelColorsList, 300, 400);
             staticPixelColorsList.Clear ();
         }
     }
     
     void setTexture(List<byte> inputPixels, int inputWidth, int inputHeight)
     {
         newTexture.Resize(inputWidth, inputHeight);
         
         float colorValue;
         
         // Convert input value to Unity's "Color"
         for(int n = 0; n < newTexture.width * newTexture.height ; n++)
         {
             colorValue = inputPixels[n] / 255.0f;
             tempColor.r = tempColor.g = tempColor.b = colorValue;
             newColors.Add (tempColor);
         }
         
         // Actually set the texture pixels
         newTexture.SetPixels(newColors.ToArray());
         newTexture.Apply();
         newColors.Clear();
     }
 }

Thanx in advance.

Comment
Add comment · Show 8
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 zach-r-d · Jun 11, 2015 at 09:33 AM 0
Share

I ran the $$anonymous$$imal project provided and memory usage stayed within the same 10kb range the whole time according to the windows task manager. Whatever's happening is not platform-universal. Try updating your graphics drivers.

avatar image Hermonirr · Jun 11, 2015 at 09:44 AM 0
Share

Thanks. Did you toggle the useNewList boolean on and off during runtime, and watched the memory usage when it was on?

avatar image zach-r-d · Jun 11, 2015 at 10:01 AM 0
Share

Ah, the boolean changes things. I am definitely seeing a very permanent memory leak now; I have to close Unity to get it to go away.

It seems that whether the list is new or not is not the deter$$anonymous$$ing factor, though. I found that the problem disappears when newWidth and newHeight are changed to constants. So perhaps it has something to do with resizing the texture. I'll keep messing around and let you know if I find anything.

avatar image Hermonirr · Jun 11, 2015 at 10:14 AM 0
Share

Thanks. Try just commenting out the newTexture.Apply() line, it will prevent the leak (along with the functionality)

avatar image zach-r-d · Jun 11, 2015 at 10:43 AM 0
Share

Interesting. I'm all but certain the resizing is what's causing it though. If you just comment out the Resize call (and make the initial texture 200x200 to prevent errors) the leak goes away.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

A node in a childnode? 1 Answer

When is a referenced texture actually loaded? 0 Answers

When is a referenced texture actually loaded? 0 Answers

the texture appear rubbish 1 Answer

Terrain not unloading 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