Is it how safe to use following function to check available memory usage in a compute?

  • Thread starter Thread starter Jane Jie Chen
  • Start date Start date
J

Jane Jie Chen

Guest
our application is WPF application.

we use the following function to get available memory usage in the system (a pc).

we notice that calling to this "RetrieveFreeMemory()" function sometime take a while.


/// <summary>
/// Retrive available memory in the system as MB unit
/// </summary>
/// <returns></returns>
public long RetrieveFreeMemory()
{
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
return Convert.ToInt64(memory);
}

Question1: if pc configured as 32-bit operation system, is it OK to call "Convert.ToInt64(memory) in 32 bit pc?

question2: if there is console application heavily using CPU and there is another application monitor the console application and calls this "RetrieveFreeMemory()" function in every two minutes to check memory usage as following. Is OK to call "RetrieveFreeMemory()" in every two minutes?

protected override string GetFileHeader()
{
StringBuilder headerStr = new StringBuilder("Date/Time," +
"Elapsed Seconds," +
"Name," +
"Working Set," +
"Base Priority," +
"Priority Class," +
"User Processor Time," +
"Privileged Processor Time," +
"TotalProcessor Time," +
"Nonpaged System Memory Size," +
"Paged System Memory Size," +
"Paged Memory Size," +
"Private Memory Size," +
"Virtual Memory Size," +
"Working Set," +
"Free Memory MB" );

return headerStr.ToString();

}


/// <summary>
/// Creates a Memory log entry.
/// </summary>
///-------------------------------------------------------------------------------------------------------------
public void LogMemoryStatsForProcess( Process process, string processName )
{
StringBuilder logString = new StringBuilder();

logString.AppendFormat("{0},", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
logString.AppendFormat("{0:F2},", DateTime.Now.Subtract(_startTime).TotalSeconds);
logString.AppendFormat("{0},", processName );
logString.AppendFormat("{0:F2},", process.WorkingSet64 / 1000000.0 );

logString.AppendFormat("{0},", process.BasePriority );
logString.AppendFormat("{0},", process.PriorityClass );

logString.AppendFormat("{0},", process.UserProcessorTime );
logString.AppendFormat("{0},", process.PrivilegedProcessorTime );
logString.AppendFormat("{0},", process.TotalProcessorTime );

logString.AppendFormat("{0:F2},", process.NonpagedSystemMemorySize64 / 1000000.0 );
logString.AppendFormat("{0:F2},", process.PagedSystemMemorySize64 / 1000000.0 );
logString.AppendFormat("{0:F2},", process.PagedMemorySize64 / 1000000.0 );
logString.AppendFormat("{0:F2},", process.PrivateMemorySize64 / 1000000.0 );
logString.AppendFormat("{0:F2},", process.VirtualMemorySize64 / 1000000.0 );
logString.AppendFormat("{0:F2},", process.WorkingSet64 / 1000000.0 );
logString.AppendFormat("{0:F2}", RetrieveFreeMemory() );
thanks!

Continue reading...
 
Back
Top