Can VBA detect the /3GB boot switch?
Microsoft have documented it here on MSDN and it's quite good, as MSDN pages go: they have explained what it is as well as how to use it.
This question is aimed at developers who use the #VBA7 and #Win64 compiler constants, and are comfortable with the API or WMI calls for reading Excel's public working set, and the memory available from the operating system.
The underlying question is: "Do I have the memory to perform this operation in one go"? and "How small will the chunks need to be if I break it up?"
The difficulty is that I cannot find any way of reading the available memory that detects the presence of the /3GB Boot switch.
Here's the code I'm using to read basic memory usage and availability:
Option Explicit
Option Private Module
Private Declare PtrSafe Function GetCurrentProcessId Lib "kernel32" () As Long
Public Function GetMemUsage()
' Returns the current Excel.Application memory usage in MB.
' This is the 'Working Set': it counts the memory footprint
' of shared Dlls. TaskMan displays the Private Working Set.
' Charles Williams explains Excel memory limits here:
' http://www.decisionmodels.com/memlimitsc.htm
' Microsoft have some documentation:
' https://support.microsoft.com/en-us/help/3066990/memory-usage-in-the-32-bit-edition-of-excel-2013-and-2016
Dim objSWbemServices As Object
Set objSWbemServices = GetObject("winmgmts:") ' WMI base class
With objSWbemServices.Get("Win32_Process.Handle='" & GetCurrentProcessId & "'")
GetMemUsage = .WorkingSetSize / 1024
End With
Set objSWbemServices = Nothing
End Function
Public Function GetMemAvailable()
' Returns the current physical RAM available
Dim objSWbemServices As Object
Set objSWbemServices = GetObject("winmgmts:") ' WMI base class
Dim obj As Object
For Each obj In objSWbemServices.InstancesOf("Win32_OperatingSystem")
With obj
GetMemAvailable = .FreePhysicalMemory / 1024
End With
Next
End Function
...And That's pretty much all. I don't have anything that looks like a working 'Excel is nearly out of memory' warning; and no leads at all on the '/3GB' flag.
VirtualAlloc
withMEM_TOP_DOWN
and see what range the result is in. I don't suggest this as a reliable technique -- specifically, if a lot of allocations have already been done the address you get back need not reflect the effective range -- but it would still be better than hunting for the/3GB
boot option specifically. This is still not nearly the same thing as determining effective memory available before thrashing, but then, neither is looking for/3GB
.