views:

73

answers:

1

How do I get the amount of free virtual memory on Mac OSX?

I'm speaking of total free virtual memory (or: free addresses) and also the biggest allocatable continous block of memory available for the current process.

(Like e.g. on Windows returned by GlobalMemoryStatus (free mem) or querieable with a combination of GetSystemInfo / VirtualQuery (biggest block))

EDIT: this is to monitor the memory fragmentation and be alerted of areas or situations, where this gets really bad. Targeting (also) 32 bit.

+1  A: 

In short, you can't. Mostly because it isn't a useful number as it changes all the time and, for 64 bit applications, if you tried to use a many GB (or many TB) allocation, you'd swap to death anyway.

What are you trying to do?

bbum
Even if there isn't a cut-and-dry facility (which shouldn't be discounted), of course it's possible to implement such a thing. As for asking why, that should be a comment.
Potatoswatter
There isn't a cut-and-dry facility and it isn't actually possible to implement without introducing race conditions or stopping the world.
bbum
Race conditions are irrelevant; every allocation changes the exact amount so find an approximation. It sounds like you could just add the amount of free physical memory to the amount of free disk space. Anyway, some people run their disks pretty full, so you could subtract a few hundred megs from that number for a safety margin and use the result to be super-well behaved.
Potatoswatter
I was referring to "race condition" as in "you can't measure it and then use it later without the value quite likely being wrong". And, no, you can't subtract a few hundred megs and have anything close to a bullet proof solution. An app's heap is often fragmented in surprising ways; single huge optimizations are a bad idea.
bbum
@bbum: The object (my point, anyway) isn't to be bulletproof, it's to possibly present a warning to the user if you're about to use a gigabyte of VM that doesn't exist. Of course they may ignore the warning, or run another intensive app after clicking through it. Anyway, that's only my idea about why it's a "useful number." I agree that the OP hasn't really described what he wants to accomplish, and it certainly isn't what I'm suggesting.
Potatoswatter
@Potatoswatter: I edited my question a while ago. Is it still not clear, what I'm trying to do?
Dodo
@Dodo: Nope. Do you want to detect whether a linear scan through the current process' address space will incur disk seeks? Don't really know what else to say… perhaps you are trying to code around some weakness of Windows.
Potatoswatter
Basically, I'm trying to code around some weakness of 32 bit OS'es. I'm (regarding this problem) totally uninterested in memory pages swapped to harddisk. I'm interested in the ocupied/not ocupied places of memory. Just now I found an
Dodo
.... interesting thing, you can't hit enter in comments... Anyway, I found that vmmap (->terminal) is returning very interesting information. Parsing this could be possible, but way to slow. However, the man page states: "The mach system call vm_region retrieves the information used by vmmap." Will look deeper into this area soon and post and answer (or an outline of it) when I found it.
Dodo
Regarding weaknesses of 32 bit OS'es: Problematic situations where the allocation of 16 MB can't be done, because its not available continously. (This is of course more problematic on 32 bit Windows where you typically only have 2 GB of address space, but 32 bit Mac OS can get crouded, too)
Dodo