0

I'm having trouble getting the "Free" Physical Memory like the one showing in Windows Task Manager. Everywhere I search, I end up getting the "Available" Physical Memory and that is not my intention.

Does anyone know how to get the "Free" Physical Memory?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Phu Minh Pham
  • 1,025
  • 7
  • 21
  • 38
  • 1
    Why do you need this information? What problem are you trying to solve? – Cody Gray - on strike May 01 '12 at 07:31
  • I want to check if the free memory is below 1000. If it is, then I don't want my program to run several processes but wait until free memory is above 1000 :) – Phu Minh Pham May 01 '12 at 07:34
  • Hmm, that doesn't seem like a good idea. I have 16 GB of RAM in my computer, and I almost never have >100 MB of "Free" memory displayed in Task Manager, despite having between 6 and 10 GB of "Available" memory. Windows just caches too aggressively for this. And there's every reason for it to do so—"free" memory is just wasted. Why do you not want to use the "available" memory metric? – Cody Gray - on strike May 01 '12 at 07:35
  • What will happen if I run a process which priority is below normal and free physical memory is, for example 1, will the user experience any lack caused by that process? – Phu Minh Pham May 01 '12 at 07:40
  • 1
    [Read this](http://blogs.msdn.com/b/oldnewthing/archive/2012/01/18/10257834.aspx). If you need a gig of ram, allocate it. If you're scared to slow the workstation down and decrease the user experience doing so, run it on a server. – CodeCaster May 01 '12 at 07:53

2 Answers2

2

You probably want the GlobalMemoryStatusEx function. See http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/01371e94-0ef4-43a8-9fcb-f3ec40e8675e/ for an example of using it from C#.

You might also find http://www.pinvoke.net/default.aspx/kernel32.globalmemorystatusex useful.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Specifically, which of the members of the `MEMORYSTATUSEX` structure contains the value corresponding to the "Free" value in Task Manager? Or are you advising that he drop that altogether and use the `dwMemoryLoad` member as his gauge? – Cody Gray - on strike May 01 '12 at 07:43
  • The documentation example reports the `ulAvailPhys` member as the amount of free memory. Documentation for `MEMORYSTATUSEX` describes this member as "The amount of physical memory currently available, in bytes. This is the amount of physical memory that can be immediately reused without having to write its contents to disk first. It is the sum of the size of the standby, free, and zero lists." – Jim Mischel May 01 '12 at 14:53
  • I tried the example from pinvoke.net, but got some weird numbers that didn't match to the free physical memory from windows task manager – Phu Minh Pham May 08 '12 at 07:25
0

I totally overlooked that you need Free memory, not available memory, so my answer is wrong, sorry. But that number doesn't mean nothing since memory used for cache will be reduced, see how windows vista, 7 memory manager works :

http://www.codinghorror.com/blog/2006/09/why-does-vista-use-all-my-memory.html

You could use GetPerformanceInfo windows API, it shows exactly the same values as Windows Task Manager on Windows 7, look at my SO answer on similar question :

C# Get used memory in %

or download source for Top Process, that is a little software that is displaying free memory and CPU usage, it's open source and totaly free, and I should say that I wrote it :)

Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • That doesn't give me the Free Physical Memory. I've tried using GetPerformanceInfo and PerformanceCounter but none of these gets me the free available memomry. – Phu Minh Pham May 01 '12 at 07:54
  • PhysicalTotal - PhysicalAvailable = free – Antonio Bakula May 01 '12 at 08:05
  • I have these information in my windows task manager: Physical Memory(MB) Total 8103 Cached 2184 Available 5775 Free 3674, how can total - available = free? – Phu Minh Pham May 08 '12 at 07:38
  • Well, I don't know how to explain, you get values PhysicalTotal PhysicalAvailable from GetPerformaceInfo and just subtract it, did you try code from that post that I linked ? – Antonio Bakula May 08 '12 at 08:17
  • Yeah and got the same as you mention earlier :) The link that you provide gave only the results of available, total and percentage used, but I need the free physical memory. If i use the available memory, windows will swap with virtual and that'll end up giving some minor lack experince with the computer that has some low performance hardware such as 1 or 2 gb ram, dual core pentium processor etc. If i can get the free physical memory, i can use those numbers to determine if the application should run or not so it doens't interfere with the users experience with the computer – Phu Minh Pham May 08 '12 at 08:34
  • As CodeCaster said, i should run it on a server and that is the hold point, not having to run the application on a server. – Phu Minh Pham May 08 '12 at 08:38
  • sorry didn't read carefull enough, yes you can't use PerfInfo for that, but that Free Psh mem doesn't mean nothing becouse memory used for cache will be reduced if some process need more memory – Antonio Bakula May 08 '12 at 09:10