C #에서 사용 가능하거나 사용되는 메모리를 얻는 방법
응용 프로그램에서 사용 가능한 RAM 또는 메모리를 어떻게 얻을 수 있습니까?
당신이 사용할 수있는:
Process proc = Process.GetCurrentProcess();
현재 프로세스를 가져 와서 사용하려면 :
proc.PrivateMemorySize64;
개인 메모리 사용을 얻으려면. 자세한 내용은 이 링크 를 참조하십시오 .
GC.GetTotalMemory 메소드 를 점검 할 수 있습니다 .
가비지 수집기에서 현재 할당 할 것으로 생각되는 바이트 수를 검색합니다.
System.Environment 에는 프로세스 컨텍스트에 매핑 된 실제 메모리의 바이트 수를 포함하는 64 비트 부호있는 정수인 WorkingSet이 있습니다.
많은 세부 정보를 원하면 System.Diagnostics.PerformanceCounter 가 있지만 설정하는 데 약간의 노력이 필요합니다.
봐 여기에 자세한 내용은.
private PerformanceCounter cpuCounter;
private PerformanceCounter ramCounter;
public Form1()
{
InitializeComponent();
InitialiseCPUCounter();
InitializeRAMCounter();
updateTimer.Start();
}
private void updateTimer_Tick(object sender, EventArgs e)
{
this.textBox1.Text = "CPU Usage: " +
Convert.ToInt32(cpuCounter.NextValue()).ToString() +
"%";
this.textBox2.Text = Convert.ToInt32(ramCounter.NextValue()).ToString()+"Mb";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void InitialiseCPUCounter()
{
cpuCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
private void InitializeRAMCounter()
{
ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
}
If you get value as 0 it need to call NextValue()
twice. Then it gives the actual value of CPU usage. See more details here.
In addition to @JesperFyhrKnudsen's answer and @MathiasLykkegaardLorenzen's comment, you'd better dispose
the returned Process
after using it.
So, In order to dispose the Process
, you could wrap it in a using
scope or calling Dispose
on the returned process (proc
variable).
using
scope:var memory = 0.0; using (Process proc = Process.GetCurrentProcess()) { // The proc.PrivateMemorySize64 will returns the private memory usage in byte. // Would like to Convert it to Megabyte? divide it by 1e+6 memory = proc.PrivateMemorySize64 / 1e+6; }
Or
Dispose
method:var memory = 0.0; Process proc = Process.GetCurrentProcess(); memory = Math.Round(proc.PrivateMemorySize64 / 1e+6, 2); proc.Dispose();
Now you could use the memory
variable which is converted to Megabyte.
For the complete system you can add the Microsoft.VisualBasic Framework as a reference;
Console.WriteLine("You have {0} bytes of RAM",
new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory);
Console.ReadLine();
참고URL : https://stackoverflow.com/questions/750574/how-to-get-memory-available-or-used-in-c-sharp
'Programing' 카테고리의 다른 글
npm 패키지가 전역 또는 로컬로 설치되었는지 확인하는 방법 (0) | 2020.07.06 |
---|---|
데이터베이스와 함께 recyclerview 사용 (0) | 2020.07.06 |
Windows의 Google 코드에서 프로젝트를 다운로드 / 체크 아웃하는 방법은 무엇입니까? (0) | 2020.07.06 |
특정 상황에서 필수 유효성 검사 속성 비활성화 (0) | 2020.07.06 |
서브 모듈 내부의 Git 서브 모듈 (중첩 서브 모듈) (0) | 2020.07.06 |