The GetProcessMemoryInfo from psapi.dll is a helpful function that can be used to monitor the memory wolframkernel.exe used, where how much memory can be used and is actually used by wolframkernel is an important factor which seems silently determines whether symbolic process will be utilized when numerical methods is not work or work well (this "intelligent" decision will sometimes crash the computer due to too heavy use of the resources), but I cannot make it work using NETLink. Any help for this will be very much appreciated. The following is one version that I have tried:
5/31/20 18:58:56 In[738]:= Needs["NETLink`"]
5/31/20 18:59:02 In[739]:= ReinstallNET[]
5/31/20 18:59:04 Out[739]= LinkObject["F:\\work\\Frank\\WolframWorkspaces\\Mathematica12.0\\SystemFiles\\\
Links\\NETLink\\InstallableNET.exe", 66545, 18]
5/31/20 18:59:32 In[740]:= GetCurrentProcess =
DefineDLLFunction["GetCurrentProcess",
"C:\\Windows\\System32\\kernel32.dll", "IntPtr", {}]
5/31/20 18:59:32 Out[740]= Function[Null,
If[NETLink`DLL`Private`checkArgCount["GetCurrentProcess", {##1}, 0],
Wolfram`NETLink`DynamicDLLNamespace`DLLWrapper1`GetCurrentProcess[##1], \
$Failed], {HoldAll}]
5/31/20 18:59:37 In[741]:= GetLastError =
DefineDLLFunction["GetLastError", "C:\\Windows\\System32\\kernel32.dll",
"DWORD", {}]
5/31/20 18:59:37 Out[741]= Function[Null,
If[NETLink`DLL`Private`checkArgCount["GetLastError", {##1}, 0],
Wolfram`NETLink`DynamicDLLNamespace`DLLWrapper2`GetLastError[##1], \
$Failed], {HoldAll}]
5/31/20 19:10:01 In[747]:= getProcessMemoryInfo6 = DefineDLLFunction["[StructLayout(LayoutKind.Sequential,
CharSet = CharSet.Auto)]
public class PROCESSMEMORYCOUNTERS
{
public uint cb;
public UInt64 PageFaultCount;
public UInt64 PeakWorkingSetSize;
public UInt64 WorkingSetSize;
public UInt64 QuotaPeakPagedPoolUsage;
public UInt64 QuotaPagedPoolUsage;
public UInt64 QuotaPeakNonPagedPoolUsage;
public UInt64 QuotaNonPagedPoolUsage;
public UInt64 PagefileUsage;
public UInt64 PeakPagefileUsage;
public PROCESSMEMORYCOUNTERS()
{
this.cb = (uint)
Marshal.SizeOf(typeof( PROCESSMEMORYCOUNTERS ));
}
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport(\"psapi.dll\",
CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool
GetProcessMemoryInfo(IntPtr hProcess, out PROCESSMEMORYCOUNTERS counters, \
uint size);"]
5/31/20 19:10:03 Out[747]= Function[Null,
If[NETLink`DLL`Private`checkArgCount["GetProcessMemoryInfo", {##1}, 3],
Wolfram`NETLink`DynamicDLLNamespace`DLLWrapper5`GetProcessMemoryInfo[##1], \
$Failed], {HoldAll}]
5/31/20 19:10:16 In[748]:= struct8 = NETNew[
"Wolfram.NETLink.DynamicDLLNamespace.DLLWrapper5+PROCESSMEMORYCOUNTERS"]
5/31/20 19:10:16 Out[748]= NETLink`Objects`NETObject$854307884761089
5/31/20 19:10:21 In[749]:= struct8@cb
5/31/20 19:10:21 Out[749]= 80
5/31/20 19:10:36 In[750]:= getProcessMemoryInfo6[GetCurrentProcess[], struct8, struct8@cb]
5/31/20 19:10:44 During evaluation of In[750]:= LinkObject::linkd: Unable to communicate with closed link LinkObject[F:\work\Frank\WolframWorkspaces\Mathematica12.0\SystemFiles\Links\NETLink\InstallableNET.exe,66545,18].
5/31/20 19:10:44 Out[750]= $Failed