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 ODINKONG · Apr 03, 2015 at 09:40 PM · memoryproceduralmemory-leak

Marching cubes Memory Leak

Ok so as titled I am generating a 5 x 5 grid of terrain objects that have perlin noise on them. The problem im having is the textures I generate on these blocks are somehow never deleted in memory. I thought that destroying the game object the texture is on would remove it from memory. Anyway Im not sure what im doing wrong here. This is the code im using to generate each block.

public class GroundSetup : MonoBehaviour {

 public float[,] heights;
 public float test;
 public float test2;
 public float scrollX;
 public float scrollY;
 Dictionary<int,float> placersx = new Dictionary<int,float> ();
 Dictionary<int,float> placersz = new Dictionary<int,float> ();
 Dictionary<int,Vector3> pos = new Dictionary<int,Vector3> ();
 Dictionary<int,GameObject> objects = new Dictionary<int,GameObject > ();
 int pkeyx = 0;
 int pkeyz = 0;
 int objectCount = 0;
 Terrain terr;
 float StartingOffset = 50000;
 GameObject tree;
 Loader loader;
 Texture2D grass;
 Texture2D grassnorm;
 Texture2D snow;
 Texture2D snownorm;
 Texture2D sand;
 Texture2D sandnorm;
 States statesScript;
 GameObject hoodie;
 

     // Use this for initialization
 void Start () {
 

     
     





 
 }

here is the part of the class where I put textures on the terrain

 public void SetupGround(){


     sand = loader.getsand ();
     sandnorm = loader.getsandN ();

     grass = loader.getgrass ();
     grassnorm = loader.getgrassN ();

     snow = loader.getsnow ();
     snownorm = loader.getsnowN ();


     SplatPrototype[] terrainTexture = new SplatPrototype[1];
     terrainTexture [0] = new SplatPrototype ();
     if (GetVert(60,60) < .4) {
                     terrainTexture [0].texture = (Texture2D)Resources.Load ("textures/sand"); 
                     terrainTexture [0].normalMap = (Texture2D)Resources.Load ("textures/sandnormal");
                     terr.materialType = Terrain.MaterialType.Custom;
     }
     if (GetVert(60,60) >= .4 & GetVert(60,60) < .8 ) {
                     terrainTexture [0].texture = (Texture2D)Resources.Load ("textures/grass"); 
                     terrainTexture [0].normalMap = (Texture2D)Resources.Load ("textures/grassnormal");
                     terr.materialType = Terrain.MaterialType.BuiltInStandard;
     }
     if (GetVert(60,60) >= .8) {
         terrainTexture [0].texture = (Texture2D)Resources.Load ("textures/snow"); 
         terrainTexture [0].normalMap = (Texture2D)Resources.Load ("textures/snownormal");
         terr.materialType = Terrain.MaterialType.Custom;
     }

     terr.materialTemplate = (Material)Resources.Load ("materials/sand");
     terr.basemapDistance = 1200f;
     terrainTexture [0].tileSize = new Vector2 (10, 10);
     
     terr.terrainData.splatPrototypes = terrainTexture;

Then when the block is no longer needed I call this

public void DeleteGround (){ foreach(KeyValuePair obj in objects){

         Destroy(obj.Value);
     }
     Destroy (gameObject);
     }

Do I need to destroy the terrain or the textures themselves? also Ive found that even with all this code commented out the textures still build up in the not saved category in the profiler. Any word of help or general cleanup tips would be greatly appreciated. Thank you.

Comment
Add comment · Show 8
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 Glurth · Apr 03, 2015 at 11:22 PM 0
Share

I'm still a bit fuzz on Unity memory management, so I'm not sure if this will actually resolve the issue but here is a tidbit that helped me a lot... Texture inherits from Unity.Object, so you should be able to use the static Object.Destory(Object) method to remove the textures from memory

UnityEngine.Object.Destroy(textureToDestroy);

avatar image ODINKONG · Apr 03, 2015 at 11:32 PM 0
Share

ok I tried adding these lines to the cleanup script.

 public void DeleteGround (){
     foreach($$anonymous$$eyValuePair<int,GameObject> obj in objects){

         Destroy(obj.Value);
     }
     Object.Destroy (sand);
     Object.Destroy (sandnorm);
     Object.Destroy (snownorm);
     Object.Destroy (snow);
     Object.Destroy (grass);
     Object.Destroy (grassnorm);
     Destroy (gameObject);
     }

as you can see there are a lot of destroy commands. this actually still somehow leaves all these textures in memory despite causing massive lag when it attempts to remove them. I am positive these are the textures being duplicated and left in memory because when I comment them out the memory leak is half as bad.

Is there some way to reference just 1 texture object for each texture and send that object to each terrain block. I thought it was a given that I would not want thousands of duplicates of the same texture.

how should I be loading these textures to not cause duplicates.

avatar image Glurth · Apr 04, 2015 at 12:20 AM 0
Share

If you are adding the texture to the objects in your dictionary,I'd think you would want those Destroys commands inside the loop, to destroy the texture on each object.

Regarding using only texture references, rather than instances: yes that would make sense.

I see you load sand with

 sand = loader.getsand (); // I assume this loads the resource

but then later on you use :

  terrainTexture [0].texture = (Texture2D)Resources.Load ("textures/sand");

Why do you NOT use?:

 terrainTexture [0].texture = sand;



avatar image maccabbe · Apr 04, 2015 at 01:28 AM 0
Share

I would check the dictionaries, does their size stay constant or continuously increase?

avatar image ODINKONG · Apr 04, 2015 at 02:24 AM 0
Share

O wow thank you glurth. I meant to do that but I have just been looking at this way to long I guess. The dictionaries are made a certain size then deleted when the object is deleted. destroying a game object does delete the objects from the scripts attached doesn't it? Or do I need to specify to remove those as well? Anyway I made the corrections and I am still destroying each texture. However They are still piling up. Is there a way to use the same object each time I apply a texture. Here is a screen shot from the profiler of the issue.

alt text

untitled-1.jpg (375.3 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ODINKONG · Apr 04, 2015 at 03:34 AM

Ok I think I got it. There are only ever 25 textures at one time now. Here is what I did.

first Glurth corrected some of the mistakes in my code, then I realized the textures were linked to a terrain data object that wasn't getting destroyed with the game object it was on. So I explicitly destroyed that. Then the program still wasn't cleaning up the textures so I had to make a class that calls these two guys like so.

 void Update () {
     if (Time.frameCount % 30 == 0)
     {
         Resources.UnloadUnusedAssets();



         System.GC.Collect();
     }

This is a hairy solution. If anyone has a better one I would love to hear it. Like for example some how just not creating all these texture objects.

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

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

Asset bundle memory leak and exception 1 Answer

Mesh memory leak 3 Answers

200MB file save causing unity memory to increase by over a gigabyte 1 Answer

Assets never unloading after loading next scene 1 Answer

0 or Blank Ref Count Items in Memory Profiler 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