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 /
  • Help Room /
avatar image
2
Question by silverfire330 · Sep 28, 2015 at 09:52 PM · texturematerialfilememory managementmemory leak

Memory leak when loading textures from file

I am at my wit's end with with out of control memory usage when loading textures from files. I am making an image slideshow which cycles through images with user input. Every time I load a new image into a texture, it increases the memory usage dramatically, and eventually causes the program to crash. I am aware that there are plenty of older questions with the same issue, however, no prior answer appears to work for me. I've tried every variation of DestroyImmediate, GC.Collect, Resources.UnloadUnusedAssets, and have even tried Application.LoadLevel to remake the scene. Even with the latter, the unused textures appear to persist in memory, and there is nothing that I can do about it. I can even do a texture.LoadImage(), then a DestroyImmediate(texture) immediately afterwards, and it still doesn't free up memory. What am I doing wrong?

  void SetImage(int index)
     {     
         DestroyImmediate(GetComponent<Renderer>().sharedMaterial.mainTexture);
         string url = "C:/Images/"+index+".jpg";
         byte [] binaryImageData = System.IO.File.ReadAllBytes(url);
         Texture2D tex = new Texture2D(8192, 4096, TextureFormat.DXT1, false);
         tex.LoadImage(binaryImageData);
         GetComponent<Renderer>().sharedMaterial.mainTexture = tex;
         Resources.UnloadUnusedAssets();
         System.GC.Collect();     
        }
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

6 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by saschandroid · Oct 02, 2015 at 10:45 AM

I don't get any memory leaks by avoiding to call new Texture2D multiple times (at least I can't see any leaks in the profiler).

 Texture2D tex;
 
 void Start()
 {
     tex = new Texture2D(4,4,TextureFormat.DXT1, false)
 }
 
 void SetImage(int index)
 {     
          string url = "C:/Images/"+index+".jpg";
          byte [] binaryImageData = System.IO.File.ReadAllBytes(url);
          tex.LoadImage(binaryImageData);
          GetComponent<Renderer>().sharedMaterial.mainTexture = tex;
 }


Comment
Add comment · 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
2

Answer by IMD · Aug 01, 2017 at 05:08 PM

So I was getting this in our slippy map app which loaded textures from online map services and although the textures are note classic Resources, the command Resources.UnloadUnusedAssets() seems to now clear my texture memory virtually immediately under Unity 5.6.1.

On the other hand CG.Collect() seemed to do nothing.

Hope this helps some folks. :)

Comment
Add comment · 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
0

Answer by silverfire330 · Oct 07, 2015 at 04:32 PM

I'm not sure if the profiler will catch it. After closing Unity, the memory is freed, so I guess Unity's garbage collection works properly at that point, but while the program is running, the memory usage goes up every time SetImage is called, to the point where it crashes. Even with the texture at 4x4, the effect is the same, it just takes longer.

Comment
Add comment · Show 4 · 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 saschandroid · Oct 15, 2015 at 09:37 AM 0
Share

Like you said, it's not about texture size ... it's about how often you (re-)create a Texture2D (see my answer). I used 4x4 only because at this point it doesn't matter how big the created texture is.

avatar image jules43 · Oct 15, 2015 at 09:57 AM 0
Share

I have now reported a bug against this problem with a $$anonymous$$imal repro project. https://fogbugz.unity3d.com/default.asp?736374_2v8g7nrue9it8rh5

avatar image silverfire330 · Oct 15, 2015 at 05:24 PM 0
Share

Hi, thanks for the input. Calling new Texture2D once in Start rather than instantiating a new one before each LoadImage did not change anything for me. The spike in memory usage that I was seeing was from the LoadImage call itself. Whether on an existing Texture2D, a new one, through www.LoadImageIntoTexture, or binaryImageData. I didn't find a solution and gave up on it. There's probably something else I'm doing wrong, but saschandroid's code copy pasted unchanged still has the issue for me.

avatar image jp_topps silverfire330 · Jun 16, 2016 at 04:08 PM 0
Share

I am running into this on 5.3.4f1, and like Silverfire, the workaround did not help me. I have isolated the Leak to Texture2D.LoadImage(System.IO.File.ReadAllBytes(path)) (strongly suspect it is specifically the LoadImage call, but still to confirm)

Also of note: I was not able to repro in the editor using similar code.

avatar image
0

Answer by jules43 · Oct 15, 2015 at 08:11 AM

I can confirm that I hit this problem today, and on Android it eventually caused an out of memory crash.

As saschandroid indicated the solution was to avoid re-creating the texture every frame, and instead just call LoadImage on the previously allocated texture.

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 jules43 · Nov 26, 2015 at 02:39 PM 0
Share

I can confirm that this bug is now fixed

avatar image ChiuanWei jules43 · May 19, 2016 at 06:43 AM 0
Share

when ? what unity version fix?? i found this memory leak in Unity5.3.4f1

avatar image
0

Answer by danw_unity · Feb 29, 2016 at 01:52 PM

There is a known issue in our database about this:

http://issuetracker.unity3d.com/issues/destroying-texture2d-doesnt-free-memory

Regards,

Dan, Unity Support

Comment
Add comment · 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
  • 1
  • 2
  • ›

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

36 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 avatar image avatar image

Related Questions

1024x1024 Texture2D Takes up over 8MB of memory 0 Answers

Hi, My textures are good on one side of the object, but very stretched out on the other side? How do I fix this? 0 Answers

Best way of combining textures 0 Answers

How do I use a sliced 2D sprite as the repeating texture on a 3D object? 0 Answers

How do I make a mesh texture transparent, but not for some objects? 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