我目前正在尝试了解如何使用WorkingSet64,PagedMemorySize64等内存属性。我编写了一个小程序,它将调试自己进程的内存消耗。
有三个类:
持久存储器转储的DTO:
public class ProcessDTO
{
public ProcessDTO(long workingSet64, long privateMemorySize64, long pagedSystemMemorySize64, long pagedMemorySize64)
{
WorkingSet64 = workingSet64;
PrivateMemorySize64 = privateMemorySize64;
PagedSystemMemorySize64 = pagedSystemMemorySize64;
PagedMemorySize64 = pagedMemorySize64;
}
public long WorkingSet64 { get; private set; }
public long PrivateMemorySize64 { get; private set; }
public long PagedSystemMemorySize64 { get; private set; }
public long PagedMemorySize64 { get; private set; }
}
进程读取器,首先强制GC运行然后创建内存转储:
internal class ProcessDumper
{
public ProcessDTO GetProcessDump(Process process)
{
GC.Collect();
GC.WaitForPendingFinalizers();
var workingSet64 = process.WorkingSet64;
var privateMemorySize64 = process.PrivateMemorySize64;
var pagedSystemMemorySize64 = process.PagedSystemMemorySize64;
var pagedMemorySize64 = process.PagedMemorySize64;
var result = new ProcessDTO(workingSet64, privateMemorySize64, pagedSystemMemorySize64, pagedMemorySize64);
return result;
}
}
最后我的控制台应用程序:
static void Main(string[] args)
{
var processDumper = new ProcessDumper();
var process = Process.GetCurrentProcess();
var before = processDumper.GetProcessDump(process);
Console.WriteLine("[WS64] Before: {0} bytes", before.WorkingSet64);
Console.WriteLine("[priv64] Before: {0} bytes", before.PrivateMemorySize64);
Console.WriteLine("[PMS64] Before: {0} bytes", before.PagedMemorySize64);
Console.WriteLine("[PSMS64] Before: {0} bytes", before.PagedSystemMemorySize64);
Console.WriteLine();
var foo = new byte[]
{
0x10, 0x99, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x04, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
};
var max = foo.Max();
Console.WriteLine("Just to do something with my object... max is {0}", max);
var after = processDumper.GetProcessDump(process);
var min = foo.Min();
Console.WriteLine("Just to do something with my object... min is {0}", min);
Console.WriteLine();
Console.WriteLine("[WS64] After: {0} bytes", after.WorkingSet64);
Console.WriteLine("[priv64] After: {0} bytes", after.PrivateMemorySize64);
Console.WriteLine("[PMS64] After: {0} bytes", after.PagedMemorySize64);
Console.WriteLine("[PSMS64] After: {0} bytes", after.PagedSystemMemorySize64);
Console.WriteLine();
Console.WriteLine("[WS64] Diff: {0} bytes", (after.WorkingSet64 - before.WorkingSet64));
Console.WriteLine("[priv64] Diff: {0} bytes", (after.PrivateMemorySize64 - before.PrivateMemorySize64));
Console.WriteLine("[PMS64] Diff: {0} bytes", (after.PagedMemorySize64 - before.PagedMemorySize64));
Console.WriteLine("[PSMS64] Diff: {0} bytes", (after.PagedSystemMemorySize64 - before.PagedSystemMemorySize64));
Console.ReadLine();
}
现在有趣的部分开始了。我假设,内存在before
和after
之间增加了 - 但内存消耗总是相同。
[WS64] Diff: 0 bytes
[priv64] Diff: 0 bytes
[PMS64] Diff: 0 bytes
[PSMS64] Diff: 0 bytes
我在这里缺少什么?我还尝试将before
,after
和diff
的输出移到最底层,以便在转储时将它们保存在内存中,但这似乎没有任何变化 - Diff总是零。
答案 0 :(得分:0)
您错过了它不计算保留内存的一部分中使用的字节数,而是计算保留内存的大小。仅当使用了所有保留内存(例如PrivateMemorySize64)时,运行时才会为该集分配更多内存。
答案 1 :(得分:0)
还有另一个问题。由于我使用了var process = Process.GetCurrentProcess();
创建的相同流程对象,因此从该对象请求的所有属性都是相同的。为after
- 转储重新创建它显示了差异
[WS64] Diff: 716800 bytes
[priv64] Diff: 593920 bytes
[PMS64] Diff: 593920 bytes
[PSMS64] Diff: 136 bytes
答案 2 :(得分:0)
我认为您只需在读取内存使用情况统计信息之前先致电+--------------------+-------------------------------------------+
| Supplier (DTRM ID) | Commodities (Use Ctrl to select multiple) |
+--------------------+-------------------------------------------+
| 12333 | Strawberry |
| 12333 | Raspberry |
| 12333 | Flamingo |
| 12333 | Snozzberry |
+--------------------+-------------------------------------------+
。