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. Dismiss Notice

know how many free memory on System? Windows Standalone

Discussion in 'Windows' started by SystemDevel, May 4, 2016.

  1. SystemDevel

    SystemDevel

    Joined:
    Oct 5, 2013
    Posts:
    36
    I need to know how many free memory are on System on real time.

    I'm on Windows Standalone Application.

    I use
    Code (CSharp):
    1.  cpuCounter = new PerformanceCounter();
    2.      cpuCounter.CategoryName = "Processor";
    3.      cpuCounter.CounterName = "% Processor Time";
    4.      cpuCounter.InstanceName = "_Total";
    5.      ramCounter = new PerformanceCounter("Memory", "Available MBytes");
    6.  
    7.         public float getAvailableRAM(){
    8.         return ramCounter.NextValue();
    9.      }
    10.  
    11.  
    12.  
    13.  
    but always return 0!!

    Any solution?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,478
  3. SystemDevel

    SystemDevel

    Joined:
    Oct 5, 2013
    Posts:
    36
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,478
    Something like this:

    Code (csharp):
    1. struct MEMORYSTATUSEX
    2. {
    3.     public int dwLength;
    4.     public uint dwMemoryLoad;
    5.     public ulong ullTotalPhys;
    6.     public ulong ullAvailPhys;
    7.     public ulong ullTotalPageFile;
    8.     public ulong ullAvailPageFile;
    9.     public ulong ullTotalVirtual;
    10.     public ulong ullAvailVirtual;
    11.     public ulong ullAvailExtendedVirtual;
    12. }
    13.  
    14. [DllImport("kernel32.dll", SetLastError = true)]
    15. static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX memoryStatus);
    16.  
    17. static ulong GetAvailableMemory()
    18. {
    19.     var structureSize = Marshal.SizeOf(typeof(MEMORYSTATUSEX));
    20.  
    21.     var memoryStatus = default(MEMORYSTATUSEX);
    22.     memoryStatus.dwLength = structureSize;
    23.  
    24.     if (!GlobalMemoryStatusEx(ref memoryStatus))
    25.         throw new Win32Exception(Marshal.GetLastWin32Error());
    26.  
    27.     return memoryStatus.ullAvailPageFile;
    28. }
    29.  
     
    SystemDevel likes this.
  5. SystemDevel

    SystemDevel

    Joined:
    Oct 5, 2013
    Posts:
    36
    Perfect!! It Works!!

    I found also this code and Works ok

    Code (CSharp):
    1. public class PerformanceDataInfo : MonoBehaviour {
    2.    
    3.     public class PerfomanceInfoData
    4.     {
    5.         public Int64 CommitTotalPages;
    6.         public Int64 CommitLimitPages;
    7.         public Int64 CommitPeakPages;
    8.         public Int64 PhysicalTotalBytes;
    9.         public Int64 PhysicalAvailableBytes;
    10.         public Int64 SystemCacheBytes;
    11.         public Int64 KernelTotalBytes;
    12.         public Int64 KernelPagedBytes;
    13.         public Int64 KernelNonPagedBytes;
    14.         public Int64 PageSizeBytes;
    15.         public int HandlesCount;
    16.         public int ProcessCount;
    17.         public int ThreadCount;
    18.     }
    19.    
    20.     [StructLayout(LayoutKind.Sequential)]
    21.     public struct PsApiPerformanceInformation
    22.     {
    23.         public int Size;
    24.         public IntPtr CommitTotal;
    25.         public IntPtr CommitLimit;
    26.         public IntPtr CommitPeak;
    27.         public IntPtr PhysicalTotal;
    28.         public IntPtr PhysicalAvailable;
    29.         public IntPtr SystemCache;
    30.         public IntPtr KernelTotal;
    31.         public IntPtr KernelPaged;
    32.         public IntPtr KernelNonPaged;
    33.         public IntPtr PageSize;
    34.         public int HandlesCount;
    35.         public int ProcessCount;
    36.         public int ThreadCount;
    37.     }
    38.    
    39.    
    40.     [DllImport("psapi.dll", SetLastError = true)]
    41.     [return: MarshalAs(UnmanagedType.Bool)]
    42.     private static extern bool GetPerformanceInfo([Out] out PsApiPerformanceInformation PerformanceInformation, [In] int Size);
    43.        
    44.     // Use this for initialization
    45.     void Start () {
    46.  
    47.     }
    48.  
    49.     static double ConvertBytesToMegabytes(long bytes)
    50.     {
    51.         return (bytes / 1024f) / 1024f;
    52.     }
    53.    
    54.    
    55.     public static PerfomanceInfoData GetPerformanceInfo()
    56.     {
    57.         PerfomanceInfoData data = new PerfomanceInfoData();
    58.         PsApiPerformanceInformation perfInfo = new PsApiPerformanceInformation();
    59.         if (GetPerformanceInfo(out perfInfo, Marshal.SizeOf(perfInfo)))
    60.         {
    61.             /// data in pages
    62.             data.CommitTotalPages = perfInfo.CommitTotal.ToInt64();
    63.             data.CommitLimitPages = perfInfo.CommitLimit.ToInt64();
    64.             data.CommitPeakPages = perfInfo.CommitPeak.ToInt64();
    65.            
    66.             /// data in bytes
    67.             Int64 pageSize = perfInfo.PageSize.ToInt64();
    68.             data.PhysicalTotalBytes = perfInfo.PhysicalTotal.ToInt64() * pageSize;
    69.             data.PhysicalAvailableBytes = perfInfo.PhysicalAvailable.ToInt64() * pageSize;
    70.             data.SystemCacheBytes = perfInfo.SystemCache.ToInt64() * pageSize;
    71.             data.KernelTotalBytes = perfInfo.KernelTotal.ToInt64() * pageSize;
    72.             data.KernelPagedBytes = perfInfo.KernelPaged.ToInt64() * pageSize;
    73.             data.KernelNonPagedBytes = perfInfo.KernelNonPaged.ToInt64() * pageSize;
    74.             data.PageSizeBytes = pageSize;
    75.            
    76.             /// counters
    77.             data.HandlesCount = perfInfo.HandlesCount;
    78.             data.ProcessCount = perfInfo.ProcessCount;
    79.             data.ThreadCount = perfInfo.ThreadCount;
    80.         }
    81.         return data;
    82.     }