How to Remove Textures from Memory (Downloaded During Play)

Hello!

I have a little project that uses photos downloaded from a server as textures for each game level. The object stays the same, but each level downloads a smallish 100-400Kb jpg image.

While looking at the profiler in XCode, running the app on the iPad, it’s obvious that each time an image is downloaded, memory usage increases 20-30Mb. After about 7 images, the app will quit (receiving memory warnings along the way).

How can I free up the memory after I’m done with each image (texture) I’ve downloaded? I don’t see the textures in the hierarchy during gameplay in Unity, so I’m not sure how to reference them for Destroy.

Thanks for any help!

4 Answers

4

I’ve answered my own question, and of course the answer is simple.

On the script attached to the object, when it’s time to switch, before loading a new texture etc, I simply added:

Destroy (renderer.material.mainTexture);

Now system memory goes up just 5MB per load, rather than 20-30. There’s still an issue it seems, but it’s not as bad at this point.

Try using Resources.UnloadAsset(yourTexture); I think this might help you.

–

i implement this, and reduce my apps memory greatly, as in my apps users may swipe to some images that requested to server. thanks!.

–

I tried Destroy(myTexture) & Resources.UnloadAsset(yourTexture) both. Destroy() works as I expected(return memory right away), however UnloadAsset() for me it doesn't really unload texture memory in time.

–

For my case, calling that helped: Resources.UnloadUnusedAssets();

check out the best compression for images in unity… perhaps //cube.renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);

A similar issue happened, my Android phones at most support 4GB memory per app using it. But for one of my Unity project, it requires at most 7GB memory, and yes, most of the memory usage is because the texture loaded in the memory. But now I am wondering if two objects are using the same texture, will the memory usage be twice of the one object case? Or just be twice?

btw: It's called a [destructor][1] not a de<b>con</b>structor. It becomes even more confusing since C# 7 now has a [feature called "deconstructor"][2]- However it has a completely different meaning. You may want to correct your blog post as well as your answer ^^. [1]: https://en.wikipedia.org/wiki/Destructor_(computer_programming) [2]: http://stackoverflow.com/questions/40906305/c-sharp-7-0-deconstructor

–

Thanks for updating the link! The blog post was interesting, especially the Parallel Stacks View.

–