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 bhart · Mar 10, 2013 at 06:03 AM · textureloadimage

texture.LoadImage doesn't release memory

Calling texture.LoadImage(imageData) causes memory to be consumed and it doesn't get released. Eventually Unity crashes. Calling these functions doesn't help: System.GC.Collect(); System.GC.WaitForPendingFinalizers();

Any ideas?

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
0

Answer by Catlard · Mar 10, 2013 at 06:28 AM

This is the function I use to load images -- I've never had a crash because of it. After the yield return StartCoroutine from this function, I use the Texture2D stored in the _textureCurrentlyWaitingFor variable. Does that help?

 private IEnumerator GetTexture2DFromPath(string path) {
     WWW www = new WWW(path);
     yield return www;
     _textureCurrentlyWaitingFor = www.texture;
 }
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 bhart · Mar 10, 2013 at 07:06 AM 0
Share

For a non-game simulation purpose, I need to load the texture in Update(). When I replace _textureCurrentlyWaitingFor with renderer.material.mainTexture it doesn't crash but the texture isn't loading. This is different behavior. With my other code the texture loaded. I'm not completely getting the 'yield return' part???

Also, an example showing how to call this function in Update() would help, as I'm pretty new to C#.

avatar image Catlard · Mar 10, 2013 at 09:28 AM 0
Share

You can't load a texture from outside of your project (which takes time, and can't happen immediately) without calling a coroutine, right? So you have to either have update be an IEnumerator function, which I don't recommend or you can load the texture in some other function while Update continues to...well...update.

What I meant by yield return is this:

Sometimes in code, you'll need to do things in a certain order, but you don't want to have a series of Coroutines calling each other. It gets disorganized and spaghetti-ish, in my opinion. So, ins$$anonymous$$d, I use the command:

 yield return StartCoroutine(CoroutineA());
 FunctionB();

This design pattern ensures that Function B will NOT be called until after CoroutineA is finished completely. Here's an example.

      private IEnumerator Get$$anonymous$$ediaImagesFor$$anonymous$$ediaInfo(string imageHalfPath) {
         
         _textureCurrentlyWaitingFor = new Texture2D(0,0);
         string absoluteImageHalfPath = imageHalfPath.Insert(0, "file://");
         
         if(File.Exists(imageHalfPath + "_cover.png")) {
             
             yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_cover.png"));
             _currentInfoBeingCreated._coverTexture = _textureCurrentlyWaitingFor;
         }
         if(File.Exists(imageHalfPath + "_inside.png")) {
             yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_inside.jpg"));
             _currentInfoBeingCreated._insideTexture = _textureCurrentlyWaitingFor;
         }
         if(File.Exists(imageHalfPath + "_backcover.png")) {
             yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_backcover.jpg"));
             _currentInfoBeingCreated._backCoverTexture = _textureCurrentlyWaitingFor;
         }
         
         if(File.Exists(imageHalfPath + "_preview.png")) {    
             yield return StartCoroutine(GetTexture2DFromPath(absoluteImageHalfPath + "_preview.png"));
             _currentInfoBeingCreated._previewImage = _textureCurrentlyWaitingFor;
         } else if(_currentInfoBeingCreated._pagePaths != null && File.Exists(imageHalfPath + _currentInfoBeingCreated._pagePaths[0])) {    
             yield return StartCoroutine(GetTexture2DFromPath("file://" + _currentInfoBeingCreated._pagePaths[0]));
             _currentInfoBeingCreated._previewImage = _textureCurrentlyWaitingFor;
         }
         
 
         
     }
 
     private IEnumerator GetTexture2DFromPath(string path) {
         WWW www = new WWW(path);
         yield return www;
         _textureCurrentlyWaitingFor = www.texture;
     }

In this example, each of the texture2ds that I am loading are loaded first, in order, but only then assigned AFTER I know the Texture2D has finished importing. Get it?

avatar image bhart · Mar 11, 2013 at 02:55 AM 0
Share

When you say "This design pattern ensures that Function B will NOT be called until after CoroutineA is finished completely. Here's an example."

Really? Or do you mean rather that CoroutineA will run until the next yield, before function B will run.

Also, this coroutine stuff seems ancillary to my immediate need which is every frame I need to load a texture, before anything else happens (and yes it needs to finish loading before anything else happens, particularly before or after rendering, but not during). But the problem I'm encountering is one of memory not being released. Can you give me help with this issue?

Thanks.

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

11 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

Related Questions

Loading images fails on Android for existing Texture2Ds. 1 Answer

Fetching files from disk at runtime 0 Answers

Assigning UV Map to model at runtime 0 Answers

cant get Texture2D.LoadImage() to work 0 Answers

Is it possible to split Texture2D.LoadImage into multiple parts? 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