Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Memory control

Discussion in 'Scripting' started by GroberMoldavan, Mar 20, 2020.

  1. GroberMoldavan

    GroberMoldavan

    Joined:
    Dec 30, 2019
    Posts:
    3
    Hello.

    I have two small questions related to "memory control".

    1. Is there any way to find how much of the RAM is currently used by the user's computer (and same for the video memory)?

    2. Is there any way to find the size of an object in memory? Excluding the hardcoding solution (that seems like a hack).

    To make it more clear, I will describe the situation I'm facing. Maybe there is another and better solution to the problem.

    In game I have some float value which is used as a setting. Based on this value I generate some amount of objects when the game starts. The object consists of some fields of basic c# types, some object references, arrays, custom buffers and compute buffers. Size of all arrays and buffers is known before object is created - it is always the same for all objects.

    So, if value is too big, user's computer might not have enough free memory to store all the objects. And I want to prevent user from setting this value to high based on current free memory.

    Hope I discribed this well enough. Thanks in advance
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You can get some useful information about the system using the SystemInfo
    https://docs.unity3d.com/ScriptReference/SystemInfo.html

    This includes the total amount of .systemMemorySize and .graphicsMemorySize.

    I'm not aware of an elegant solution for getting the currently used amounts. As for graphics memory, it's fair to assume that you can use it all for your game. For system memory i would honestly just assume the user has 'enough' in this day and age. So as for my opinion, i believe you may be over-engineering this. You can warn the user that a certain action or option may require approximately X MB of ram to be run efficiently, but i would not do more than that.
    And even for this, i would probably just approximate the numbers, ie adding up big consumers like models or large arrays.
     
    matkoniecz likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Usually you don't need to worry about "free memory" because page files have been standard for decades, and widespread adoption of SSD's have even made page files not so horrible anymore.
     
  5. GroberMoldavan

    GroberMoldavan

    Joined:
    Dec 30, 2019
    Posts:
    3
    Thanks all of you for replies.

    I know about system info, but the problem is that it shows the total amout of memory in computer, which is not necessarily free. And I agree, for RAM this may not be a problem, since OS will hadle all memory management. But I'm not so shure about video memory, tbh. I had a weird bug, where my screen will go totally black with a small square of white pixels in the bottom. And I think that this happend because of video memory overflow.

    Maybe yes, I'll just stick with "user notification" solution.


    Actually I found the hacky way to find object size in memory. You can simply get the currently allocated memory, create the "fake" object and get allocated memory again. Then all you have to do is simply subtract theese two values. But this solution does not count the stack allocations, as I've noticed

    Code (CSharp):
    1. long totalMemBefore = GC.GetTotalMemory(false);
    2.  
    3. UserObject usrObj = new UserObject();
    4.  
    5. /* some initialization to allocate all the buffers */
    6.  
    7. long usrObjMemSize = GC.GetTotalMemory(false) - totalMemBefore;
     
    Last edited: Mar 21, 2020
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Can someone clarify how exactly GetTotalMemory works? Since it's called on GC (= garbage collector), which deals with references on the heap, i'm curious if it would even include memory allocated on the stack. In case it doesnt, it wouldnt necessarily give a good approximation of the object size, but i dont exactly feel confident on this topic either.
    Maybe someone can clarify this.
     
  7. GroberMoldavan

    GroberMoldavan

    Joined:
    Dec 30, 2019
    Posts:
    3
    Yes, this method does not include stack memory in calculations, if I'm correct. I've tried to use this method with some basic types and structs and the resulting size was zero

    So right now it looks like you have to hardcode at least the "stack size" of an object, but the heap part can be approximated using GetTotalMemory
     
    Last edited: Mar 21, 2020