Search Unity

Memory Size of Undo List

Discussion in 'Scripting' started by RedHotFlashman, Oct 19, 2019.

  1. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    35
    I am making an UndoRedoManager for a mobile RaceTrackEditor. The UndoRedoManager executes a Command and stores the command in a List so I can Undo or Redo it. I would like to know how much memory the List is using. How do I do this?

    I tried something like the code below but the logged amount stays the same, even with a million copies.
    void TestMemory() {
    for (int i = 0; i < 1000000; i++) {
    testList.Add( new MoveControlPointCommand( from, to, controlPoint );
    }
    Debug.Log( SystemInfo.systemMemorySize+" Mb" );
    }
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    systemMemorySize
    gives you the amount of memory in the computer, not how much is being used by your program. Try using the memory profiler.
     
  3. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    35
    Yes, profiler is a nice tool, but i want to notify low-end users when they reach a limit. The user would then be suggested to save the progress, and undo gets reset.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You should use the built-in low memory warning event for this purpose.

    Additionally, it is probably more user-friendly to instead/additionally use a circular buffer for your undo states, if you're concerned about the number of saved undos. This will allow the oldest undo states to be overwritten after a certain number of states is reached.
     
    Last edited: Oct 21, 2019
    RedHotFlashman and Ryiah like this.