I have very simple trivial C# code compiled to .NET exe
assmebly:
using System;
class Program
{
public static void Main()
{
Console.WriteLine("test");
}
}
The compiled assembly runs on my desktop Windows 8.1
machine for about 0.02
seconds every time, but on the production Windows Server 2012 R2
it runs for 1.05
seconds.
Here is the PowerShell I am testing with:
$sw = [Diagnostics.Stopwatch]::StartNew()
for ($i=1; $i -le 10; $i++)
{
.\cs.exe
}
$sw.Stop()
$sw.Elapsed
Output on my local machine:
TotalMinutes : 0.00391715666666667
TotalSeconds : 0.2350294
TotalMilliseconds : 235.0294
.NET versions on my local machine:
PSChildName Version
----------- -------
v2.0.50727 2.0.50727.4927
v3.0 3.0.30729.4926
Windows Communication Foundation 3.0.4506.4926
Windows Presentation Foundation 3.0.6920.4902
v3.5 3.5.30729.4926
Client 4.5.51641
Full 4.5.51641
Client 4.0.0.0
Output on the server:
TotalMinutes : 0.172228565
TotalSeconds : 10.3337139
TotalMilliseconds : 10333.7139
.NET version on server:
PSChildName Version
----------- -------
Client 4.5.51650
Full 4.5.51650
Client 4.0.0.0
Environment variables on server:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\*********\AppData\Roaming
CLIENTNAME *********
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME *********
ComSpec C:\Windows\system32\cmd.exe
COR_ENABLE_PROFILING 7
COR_PROFILER {8019fee9-9590-4bd4-b2c9-815628f80f0f}
CORECLR_ENABLE_PROFILING 0
FP_NO_HOST_CHECK NO
HOMEDRIVE C:
HOMEPATH \Users\*********
JAVA_HOME C:\Progra~1\Java\jdk1.8.0_45
LOCALAPPDATA C:\Users\*********\AppData\Local
LOGONSERVER \\*********
NUMBER_OF_PROCESSORS 12
OS Windows_NT
Path C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System3...
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 3f02
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PSModulePath C:\Users\*********\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPow...
PUBLIC C:\Users\Public
SESSIONNAME RDP-Tcp#126
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\ACADWE~1\AppData\Local\Temp\2
TMP C:\Users\ACADWE~1\AppData\Local\Temp\2
USERDOMAIN *********
USERDOMAIN_ROAMINGPROFILE *********
USERNAME *********
USERPROFILE C:\Users\*********
windir C:\Windows
What can cause this slowness of 1 second?
Is the JIT compiler the problem?
Is there any CLR-related service that should be ran in the server?
Are there any application validation checks that can be disabled?
This problem happens only to .NET exe files. Simple executables compiled with C++ run for 0.01
seconds
Edit:
I've tried to run ngen.exe ExecuteQueuedItems
as suggested in the comments but nothing changed. Here is the result from the command:
PS C:\Windows\system32> C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe ExecuteQueuedItems
Microsoft (R) CLR Native Image Generator - Version 4.0.30319.33440
Copyright (c) Microsoft Corporation. All rights reserved.
All compilation targets are up to date.
I have found the problem.
It seemed that the server had dotMemory installed.
Also there were the following environment variables set:
COR_ENABLE_PROFILING 7
COR_PROFILER {8019fee9-9590-4bd4-b2c9-815628f80f0f}
CORECLR_ENABLE_PROFILING 0
After uninstalling dotMemory and deleting these environment variables everything is working as expected (0.02
seconds per C# application run)