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 gretty · Dec 18, 2013 at 04:53 AM · texturememory-leak

Runtime Generated Textures Not Releasing Memory

Hello

My textures that I download at runtime are not releasing memory when I destroy them. Can you tell me what is going wrong and how I can release the texture (.png) memory?

I have an example scene and 1 script that very nicely & simply demonstrates the problem. When the user presses the space bar, we download a texture (satellite image) and apply it to a cube (we delete the cubes existing texture to release that memory).

I am profiling my .exe using the task manager and it shows that whenever I press space bar that the memory always increases (by about 1mb) and doesn't decrease or stay constant. So the texture memory isn't being released.

Memory Profile Results:
- Game Open: 27mb
- First Space bar press: 30mb
- Second Space bar press: 31mb

Any advice on how I can release the textures memory or achieve any memory savings?

My script and scene can be downloaded here:

Script Scene

Simple Script:

 void Update () {
     
     if (Input.GetKeyDown(KeyCode.Space)) {
         StartCoroutine( downloadFile("http://maps.google.com/maps/api/staticmap?center=-33.7600944982221,151.219708834037&zoom=18&size=512x512&scale=1&maptype=satellite&format=png&sensor=true&") );
         guiTxt.text = "Downloading Texture";
     }
 }
 
 IEnumerator downloadFile(string url) {
     
     WWW www = new WWW(url); 
             
     while(!www.isDone)
         yield return www;
     
     if (www.isDone) {
         DestroyImmediate(GameObject.Find ("Cube").renderer.material.mainTexture, true);
         System.GC.Collect();
         Resources.UnloadUnusedAssets();
         GameObject.Find ("Cube").renderer.material.mainTexture = compressTexture(www);  //www.texture;
         guiTxt.text = "New Texture Applied";
     }
 }
 
 Texture compressTexture(WWW www) {
     // Post: My method to compress textures that are currently used/displayed in game
     //          Without this code the texture uses 1.1 megabytes!!! With it, it uses about 300k
     
     Texture2D texture = new Texture2D(2, 2, TextureFormat.RGB24, false); // size params can be anything AFAIK
     www.LoadImageIntoTexture(texture);
     texture.Apply(false, false);
     texture.Compress(true);
     return texture;
 }


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 iwaldrop · Dec 18, 2013 at 06:29 AM 0
Share

When sharing project resources you should export a package ins$$anonymous$$d of just a scene. The scene doesn't actually contain the script or it's settings, but an exported package will include all of the scene's dependancies as well as their scene-specific idiosyncrasies.

The WWW class has a textureNonReadable flag which saves memory as well, because Unity doesn't need to store two versions of the texture in memory (one to edit, and the original). Based from what's available in the documentation I'm not sure that the LoadImageIntoTexture method does this or not, so you may want to do some more sleuthing on your own. Just a heads-up on that vs the usual www.texture field.

Finally, the following is a quick and dirty reworking of your code that demonstrates the memory is reclaimed without having to call Resources.Unload or do a Garbage Collection.

 using UnityEngine;
 using System.Collections;
 using System.Configuration;
 
 public class WWWTextureTest : $$anonymous$$onoBehaviour
 {
     public GameObject cube;
 
     const string I$$anonymous$$AGE_URL = "http://maps.google.com/maps/api/staticmap?center=-33.7600944982221,151.219708834037&zoom=18&size=512x512&scale=1&maptype=satellite&format=png&sensor=true&";
 
     private bool forceFullCollection;
 
     void Update ()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
                 StartCoroutine(DownloadImage(I$$anonymous$$AGE_URL, forceFullCollection));
             else
                 StartCoroutine(DownloadImage(I$$anonymous$$AGE_URL));
         }
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
             forceFullCollection = !forceFullCollection;
     }
 
     IEnumerator DownloadImage(string url, bool forceFullCollection)
     {
         yield return StartCoroutine(DownloadImage(url));
         Debug.Log(string.Format("Total $$anonymous$$emory Used ({0} collection): {1}", forceFullCollection ? "full" : "partial", System.GC.GetTotal$$anonymous$$emory(forceFullCollection)));
     }
     
     IEnumerator DownloadImage(string url)
     {
         WWW www = new WWW(url);
         while(!www.isDone)
             yield return www;
         cube.renderer.material.mainTexture = www.textureNonReadable;
     }
 }
 

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

19 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

Related Questions

Download images from url, save to device, load into UGUI 4 Answers

New blank project with 3 leaked textures ??? 1 Answer

Assigning UV Map to model at runtime 0 Answers

Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers

Corrupted/disarrange 2D Texture after app is built 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