除非启动应用程序

时间:2017-04-12 19:43:00

标签: c# .net profiling jetbrains-ide dotmemory

我有以下内容来启动分析API:

while (!MemoryProfiler.IsActive || !MemoryProfiler.CanControlAllocations)
{
    _presenter.WriteLine("Press enter to try to attach.");
    Console.ReadKey();
    Thread.Sleep(250);
}

MemoryProfiler.EnableAllocations();

但是,如果我运行此操作并将dotMemory附加到其中,则MemoryProfiler.CanControlAllocations始终为falseMemoryProfiler.IsActive变为true)。

如果我让dotMemory启动应用程序,那么它会按预期工作,并且两者都评估为true

但是,如果我将while替换为Console.Read()if,请执行以下操作:

Console.WriteLine("Press enter when connected.");
Console.ReadLine();

if (MemoryProfiler.IsActive && MemoryProfiler.CanControlAllocations)
{
    MemoryProfiler.EnableAllocations();
}

然后它工作正常。

发生了什么事? while循环如何改变这样的行为?

更奇怪的行为

如果我运行以下内容(if已注释掉)

Console.WriteLine("Press enter when connected.");
Console.ReadLine();

Console.WriteLine($"MemoryProfiler.IsActive: {MemoryProfiler.IsActive}");
Console.WriteLine($"MemoryProfiler.CanControlAllocations: {MemoryProfiler.CanControlAllocations}");

//if (MemoryProfiler.IsActive && MemoryProfiler.CanControlAllocations)

MemoryProfiler.EnableAllocations();

Console.WriteLine("Attached.");

然后我得到输出:

Press enter when connected.

MemoryProfiler.IsActive: True
MemoryProfiler.CanControlAllocations: False

然后调用EnableAllocations()时抛出异常:

JetBrains.Profiler.Windows.Api.ProfilingApiException: 'Method isn't supported'

我希望这是因为MemoryProfiler.CanControlAllocationsfalse

然而,如果我取消注释if语句:

Console.WriteLine("Press enter when connected.");
Console.ReadLine();

Console.WriteLine($"MemoryProfiler.IsActive: {MemoryProfiler.IsActive}");
Console.WriteLine($"MemoryProfiler.CanControlAllocations: {MemoryProfiler.CanControlAllocations}");

if (MemoryProfiler.IsActive && MemoryProfiler.CanControlAllocations)

    MemoryProfiler.EnableAllocations();

Console.WriteLine("Attached.");

然后一切都恢复正常,我得到了预期的输出:

Press enter when connected.

MemoryProfiler.IsActive: True
MemoryProfiler.CanControlAllocations: False
Attached.

1 个答案:

答案 0 :(得分:1)

cat始终MemoryProfiler.CanControlAllocations 处于附加模式,因为无法收集分配。请参阅false

中的COR_PRF_ALLOWABLE_AFTER_ATTACHCOR_PRF_ENABLE_OBJECT_ALLOCATEDCOR_PRF_MONITOR_OBJECT_ALLOCATED标记

P.S。 corprof.idlMemoryProfiler.EnableAllocations()将始终在附加模式下抛出异常。

以下代码适用于启动和附加模式:

MemoryProfiler.DisableAllocations()

P.P.S。这是官方JetBrains的回答: - )