Link to home
Start Free TrialLog in
Avatar of TeLieh
TeLieh

asked on

How to prevent using virtual memory

Dear All:
    I have to allocate large memory to do date transfer through SCSI equipment.I allocate memory through standard memory function and sometimes get the virtual memory.
    The SCSI action will crash the system using virtual memory.  
    Is there any method to prevent get virtual memory or to recognize how much physical memory is left in my application's heap So that I can allocate memory smaller than that size
   Thank In advanced

Best Regards
Te-Lieh
.    
Avatar of Alex Curylo
Alex Curylo

Well, that's a start, but that's not going to stop your crashes by itself. The real trick to watch out for here is that other applications or the System might cause a page out your buffer behind your back, even if you are careful about physical memory size. What you should do is around your sensitive calls call

FUNCTION HoldMemory (address: UNIV Ptr; count: LongInt): OSErr;

to ensure that your buffer is actually in RAM until you call UnholdMemory().

Note that this is not just a good idea, it's the LAW. The VM chapter of IM says

"During the time that the Macintosh is handling a page fault, it is critical that no other page faults occur. Because the system performs no other work while it is handling a page fault, only code that runs as a result of an interrupt can generate a second page fault. For this reason, you must call the HoldMemory function on buffers or code that are to be referenced by any interrupt service routine. You must call this function at noninterrupt level because the MemoryDispatch calls may cause movement of logical memory or physical memory and possible I/O."

Your SCSI read buffer almost certainly is referenced by an interrupt service routine. And that would explain your crashes quite nicely :)

I strongly suggest you read through the Virtual Memory chapter of Inside Macintosh, which documents the above and probably lots of other stuff you should know, found on the Web at

http://developer.apple.com/techpubs/mac/Memory/Memory-151.html

Now, as for the actual question you asked, that's easy, just call Gestalt(), I'm sure you've run into that routine already, with the gestaltPhysicalRAMSize selector.
ASKER CERTIFIED SOLUTION
Avatar of Alex Curylo
Alex Curylo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial