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 Balthatczar · Jan 23, 2014 at 09:29 PM · texture2dmemory managementmemory-leakdimensions

cleanup allocated memory for texture2D

I'm wondering if you create a new Texture2D and fill it up with something and then later recreate them and fill them up again, how do you release the memory allocated the previous time? So if for example you want to keep the same name but recreate the texture with new dimensions. I'm writing a rather lengthy routine but this is a problem i have, as my application eventually crashes. If i create the Texture2D only once this problem doesn't occur. So i can only conclude that if you construct a new Texture2D... memory is allocated at some point but if you then construct it again with other dimensions somehow there's a need to cleanup the memory previously allocated but i don't see a clear way to do this.

Does this make any sense, and if so what can be done about it?

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 Slobdell · Jan 23, 2014 at 11:20 PM

What language are you using? In C# you rely on garbage collection to free up memory and it does it when it feels like it. Best you can do is make sure all references to it have been destroyed. I have a feeling, however that something you're doing is extremely inefficient if that's your problem but unless you post code and explain more it's impossible to know.

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 Balthatczar · Jan 23, 2014 at 11:39 PM 0
Share

I'm using C# and i actually did find out a way to free allocated memory by setting texture to null and then use UnloadUnusedAssets and garbage collection to clear it all out. This actually did solve it to the point that it can be run without issues in Unity Editor but an iOS build for iPad will still eventually crash though it will take a while longer for that to happen now. The complete function is over 260 lines long so i'm not going to post it, but i went over it many many times and i'm pretty sure there's nothing inefficient about it. I've also found another solution using different scenes to solve it fortunately.

I did not end up destroying the references in my previous method and i suspect that is whats causing problems. By references you mean for example if the texture is assigned to the material of a GameObject, that this GameObject should also be destroyed correct?

avatar image Slobdell · Jan 23, 2014 at 11:49 PM 0
Share

Yeah. The object doesn't have to be destroyed but the reference should be set to null.

 String example = "some words";
 String another = example;

Now there are two references to the same memory location. When garbage collector comes around it will see there are still references to that spot and leave it be. In order for that memory to be freed you need example = null; another = null;

A lot of times it's easy to be doing work with one and completely forget about the other and it's just a reference that sits around and clogs up memory.

avatar image Balthatczar · Jan 24, 2014 at 01:40 AM 2
Share

Yeah finally figured it all out, as it happens i still needed to take care of this. So for anyone else that might be reading this, in a coroutine don't forget to yield UnloadUnusedAssets(); because if you don't its very likely the unloading will be incomplete.

not very difficult:

AsyncOperation op = Resources.UnloadUnusedAssets();

while (!op.isDone) yield return 0;

GC.Collect();

avatar image drudiverse Balthatczar · Dec 14, 2015 at 04:20 PM 0
Share

I tried as you suggest It stays the same, I also have www.textures running in a coroutine. The memory goes up every time I try a different fix which unloads assets and things.

I have started a new question on the topic: http://answers.unity3d.com/questions/1112217/texture2d-wwwtexture-memory-leak-not-fixed.html

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

20 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

Related Questions

Kinect SensorData Memory Leak 1 Answer

ManagedStaticReferences when unload unused resources 0 Answers

Detect WWW Image bitmap dimensions? 2 Answers

Is this memory usage too much for a mobile game? 1 Answer

Memory allocation issue 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