Search Unity

Terrain map causes memory heap. How can i fix it?

Discussion in 'Web' started by bekiryanik, Feb 3, 2021.

  1. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Hello,

    I am currently working on a WebGL project. The project file size is around 70-75 MB. Around 10-15 MB is obtained by the Unity's terrain map that I used with my project. The terrain map is 4000x4000. You can see the terrain settings in the picture attached. Each time I restart the screen, the memory usage increases, and after 3 or 4 screen restart, I get out of memory error. What kind of terrain settings should I use to prevent memory error?

    By the way, when I delete terrain from the screen and when I build my game, I never get a memory error. I finally found that the memory error is caused by terrain. It is not kind of possible for me to delete the terrain from the game because it is needed inside the game

    Thanks in advance.
     

    Attached Files:

  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Can you post a screenshot and/or browser console logs about the memory error? First thing to do here is to identify which heap type is running out of memory.
     
  3. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Thanks for your reply jukka_j! I attached the error as a screenshot.

    Edit: Do you want me to post the screenshot of console log in here? I don't know how to do it, but I can search for it and prepare it for you.

    Second edit:
    I found which part of the code causes memory heap. I am not sure but the profiler says that when the scene is loaded this code piece uses memory too much :)

    Code (CSharp):
    1.  
    2. void GetTerrainData(){
    3.  
    4.         if (!Terrain.activeTerrain)
    5.             return;
    6.        
    7.         mTerrainData = Terrain.activeTerrain.terrainData;
    8.         alphamapWidth = mTerrainData.alphamapWidth;
    9.         alphamapHeight = mTerrainData.alphamapHeight;
    10.  
    11.         mSplatmapData = mTerrainData.GetAlphamaps(0, 0, alphamapWidth, alphamapHeight);
    12.         mNumTextures = mSplatmapData.Length / (alphamapWidth * alphamapHeight);
    13.  
    14.     }
    15.  
     

    Attached Files:

    Last edited: Feb 3, 2021
  4. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Ok, that error message suggests that the browser is utilizing more than 2GB of memory, which is currently the max limit for WebAssembly pages.

    When you use Unity's memory profiler on natively Windows or macOS in the editor, and reload the level with the terrain, can you see the memory increasing for the terrain? If so, then that suggests that something in your code is keeping references to the previous terrains alive, and leading to multiple copies of it in memory.
     
    bekiryanik likes this.
  5. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    The thing that increases my memory usage was GetTerrainData() code which I share above. Each time I reload the screen, the code is called and this increases the memory. I deactivated it for now and now I going to create a copy of it and I will try my game to read that copy on each reload.

    Thank you so much for your interest!