Need perform free available memory every 1sec, so I use method and timer tick, but it is not changing, it shows always 8081MB in the label text. So how to make it to it check every 1sec? Because using computer memory change also. Here is my code:
// Get Available Memory
public void getAvailableRAM()
{
ComputerInfo CI = new ComputerInfo();
ulong mem = ulong.Parse(CI.TotalPhysicalMemory.ToString());
lbl_Avilable_Memory.Text = (mem / (1024 * 1024) + " MB").ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
// Get Available Memory Timer
ram_timer.Enabled = true;
// end memory
}
private void ram_timer_Tick(object sender, EventArgs e)
{
getAvailableRAM();
}
The difference between free memory vs. available memory in Linux is, that free memory is not in use and sits there doing nothing. While available memory is used memory that includes but is not limited to caches and buffers, that can be freed without the performance penalty of using swap space.
Free ram is ram that hasn't been allocated yet. Programs that aren't using ram keep it allocated for later because it's faster to reuse already allocated ram than free ram and reallocate it later. If a new process needs memory but the amount of free ram is low, some of the available memory will be deallocated.
Try with this...
Include a reference to the Microsoft.VisualBasic
dll:
using Microsoft.VisualBasic.Devices;
...and then update your label as follows:
lbl_Avilable_Memory.Text = new ComputerInfo().AvailablePhysicalMemory.ToString() + " bytes free";
...or...
lbl_Avilable_Memory.Text = (ComputerInfo().AvailablePhysicalMemory / 1048576) + "mb free";
Notes:
AvailablePhysicalMemory
property of the ComputerInfo
class in preference over the TotalPhysicalMemory
property you used previously.getAvailableRAM()
method isn't required. Replace the call in ram_timer_tick
with lbl_Avilable_Memory.Text = (ComputerInfo().AvailablePhysicalMemory / 1048576) + "mb free";
get
are expected to return a value. If the method was to stay then I'd recommend renaming it to SetLbl_Avilable_Memory()
instead.available
incorrectly in your label name.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With