GlobalMemoryStatus 函数报告 NUnit 控制台中的可用页面文件无效 运行

GlobalMemoryStatus function reports invalid available page file in NUnit console run

我正在 NUnit 测试中调用 GlobalMemoryStatus

[TestFixture]
public class UnitTest1
{
    [DllImport("kernel32.dll")]
    private static extern void GlobalMemoryStatus([In, Out] MemoryStatus status);

    [Test]
    public void TestMethod1()
    {
        var mem = new MemoryStatus();
        GlobalMemoryStatus(mem);
        Assert.AreNotEqual(0, mem.dwAvailPageFile);
    }

    [StructLayout(LayoutKind.Sequential)]
    internal class MemoryStatus
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public uint dwTotalPhys;
        public uint dwAvailPhys;
        public uint dwTotalPageFile;
        public uint dwAvailPageFile;
        public uint dwTotalVirtual;
        public uint dwAvailVirtual;
    }
}

当 运行 在 Visual Studio 内时,此测试工作正常。

但是如果用nunit-console.exe执行会失败:

nunit\nunit-console.exe bin\Debug\UnitTestProject1.dll /framework:net-4.5

使用此输出:

PS C:\UnitTestProject1> nunit\nunit-console.exe bin\Debug\UnitTestProject1.dll /framework:net-4.5
NUnit-Console version 2.6.4.14350
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
   OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1
  CLR Version: 2.0.50727.5466 ( Net 3.5 )

ProcessModel: Default    DomainUsage: Single
Execution Runtime: net-4.5
.F
Tests run: 1, Errors: 0, Failures: 1, Inconclusive: 0, Time: 0.1470761 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

Errors and Failures:
1) Test Failure : UnitTestProject1.UnitTest1.TestMethod1
     Expected: not 0
  But was:  0

如果我使用 MSTest 它工作正常:

mstest /testcontainer:bin/debug/unittestproject1.dll

使用此输出:

C:\UnitTestProject1>mstest /testcontainer:bin/debug/unittestproject1.dll
Microsoft (R) Test Execution Command Line Tool Version 11.0.61030.0
Copyright (c) Microsoft Corporation. All rights reserved.

Loading bin/debug/unittestproject1.dll...
Starting execution...

Results               Top Level Tests
-------               ---------------
Passed                UnitTestProject1.UnitTest1.TestMethod1
1/1 test(s) Passed

Summary
-------
Test Run Completed.
  Passed  1
  ---------
  Total   1
Results file:  C:\UnitTestProject1\TestResults\ddimitrov_BPCANALYTICS01 2015-04-02 14_09_16.trx
Test Settings: Default Test Settings

我正在运行在具有 6GB RAM 的 Windows Server 2008 R2 Standard x64 机器上进行测试。同样的 nunit-console 测试在 Windows 8.1.

上运行良好

你知道我可能做错了什么导致这个 Win32 函数报告不正确的数据吗?

这是来自 MSDN 的评论:

On computers with more than 4 GB of memory, the MEMORYSTATUS structure can return incorrect information, reporting a value of –1 to indicate an overflow. If your application is at risk for this behavior, use the GlobalMemoryStatusEx function instead of the GlobalMemoryStatus function.

结构是 defined 像这样:

typedef struct _MEMORYSTATUS {
  DWORD  dwLength;
  DWORD  dwMemoryLoad;
  SIZE_T dwTotalPhys;
  SIZE_T dwAvailPhys;
  SIZE_T dwTotalPageFile;
  SIZE_T dwAvailPageFile;
  SIZE_T dwTotalVirtual;
  SIZE_T dwAvailVirtual;
} MEMORYSTATUS;

SIZE_T 成员是指针大小的,这意味着您的声明在 64 位进程中是不正确的。对这些成员使用 UIntPtr