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
0
Question by drudiverse · Dec 14, 2015 at 04:17 PM · www

Texture2d www.texture Memory leak in coroutine?

I am getting WWW images onto some cubes from a google image search and the memory goes above 1gb after 20 pics, and when i stop Play the memory only goes back to 900mb until i restart the Unity Editor.

I have checked all the forums for solutions and have tried about 10 different solutions.

Can you find a logical reason why www.textures are being lost inside memory and i can't delete them?

  var tex2d[] = new Textre2D[100];
  var ZZ =0;
  var foundpics = 0;


  function imgcube( imgURL : String , cubename : String ){
  //function called inside loop for 10 imgURL's
 var loopbreak = 0;
 var www = new WWW(imgurl);

 yield www;
 ZZ ++;
 tex2d[ZZ] = www.textureNonReadable;
 
 while( loopbreak < 10 )
 {
     yield WaitForFixedUpdate();
     loopbreak ++;
     if( www.isDone == true )
     if(tex2d[ZZ].width < 256 || tex2d[ZZ].height < 256)            
     {break;} 
     else 
     {
     
         foundpics +=1;
         var cube = GameObject.CreatePrimitive( PrimitiveType.Cube ) ;
         cube.GetComponent(Renderer).material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
         cube.transform.position = Vector3(-4+foundpics%10,Mathf.Floor(foundpics/10),-20);
         www.LoadImageIntoTexture(cube.GetComponent(Renderer).material.mainTexture);
         //cube.renderer.material.mainTexture = tex2d[ZZ];

         //Destroy(cube);
         (cube.renderer.material.mainTexture);
         //Destroy(tex2d[ZZ]);
         tex2d[ZZ] = null;
         Resources.UnloadAsset(tex2d[ZZ]);

         DestroyImmediate(www.texture);
         DestroyImmediate(tex2d[ZZ]);
         
         var  op : AsyncOperation = Resources.UnloadUnusedAssets();
         while (!op.isDone) yield WaitForFixedUpdate();
         System.GC.Collect();
         
         www.Dispose();
         Resources.UnloadUnusedAssets();
         break;
         
     }
     
     if (loopbreak >10) break;
 }
     

}

Comment
Add comment · Show 1
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 drudiverse · Dec 14, 2015 at 04:43 PM 0
Share

I checked this out on unity 4.6 and 5 for the same test, 4.6 goes up to 600$$anonymous$$B after 25 images. 5 goes well beyond 1GB and crashes, using the same code.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by theylovegames · Feb 01, 2017 at 09:07 AM

Memory seems to hold steady with this.

         // Use this for initialization
         IEnumerator Start()
         {
             Texture2D oldTexture = null;
             while (true)
             {
                 WWW www = new WWW(_mUrl);
                 yield return www;
                 Texture2D newTexture = www.texture;
                 _mMaterialInstance.mainTexture = newTexture;
                 www.Dispose();
                 if (oldTexture)
                 {
                     DestroyImmediate(oldTexture);
                 }
                 oldTexture = newTexture;
                 yield return null;
             }
         }
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 divyajalan · Aug 26, 2019 at 09:10 AM 1
Share

I used Destroy ins$$anonymous$$d of DestroyImmediate but that fixed the issue. Was struggling on this for quite some time. Thanks for your answer!

avatar image JasonsFreeTime · Sep 19, 2019 at 08:11 PM 0
Share

This worked for me as well! Pretty amazing. This is actually giving me near real-time imagery from an IP camera. For my needs I added

while (true) { yield return new WaitForSeconds(delayTime); ...

just as a mechanism for reducing network bandwidth if needed.

Thanks!

avatar image
0

Answer by Dave-Carlile · Dec 14, 2015 at 05:06 PM

From some research on similar problems, it seems that you don't want to ever reference WWW.texture since that creates a resource behind the scenes that never goes away.

http://answers.unity3d.com/questions/474421/wwwtexture-dispose-didnt-work-causing-memory-leak.html

http://answers.unity3d.com/questions/892703/wwwtexture-vs-loadimageintotexture.html

Your call to DestroyImmediate(www.texture) is actually creating the instance it then tries to destroy.

Also, how big are your textures? They are uncompressed in memory when you load them, so for example a jpeg file might be small, but uncompressed into a full sized uncompressed image could be very large.

Comment
Add comment · Show 1 · 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 drudiverse · Dec 14, 2015 at 06:40 PM 0
Share

Thanks! I read that option and i obviously didnt implement the fix properly first time.

The memory has a totally different performance using LoadIntoTexture and not even reading from www.texture.

Cool.

So www.texture is actually redundant, as it should always be the other option and set to readable in another option if necessary.

Lots of 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

34 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 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

Problem streaming large movie 1 Answer

How do I add an event listener for the WWW class? 1 Answer

Coroutine needed here? 1 Answer

Download a file to project 0 Answers

Question about screenshots and facebook integration 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