Skip to content

Latest commit

 

History

History
31 lines (20 loc) · 903 Bytes

File metadata and controls

31 lines (20 loc) · 903 Bytes

Pattern: Use of Process.GetCurrentProcess().Id

Issue: -

Description

System.Diagnostics.Process.GetCurrentProcess().Id is expensive:

  • It allocates a Process instance, usually just to get the Id.
  • The Process instance needs to be disposed, which has a performance impact.
  • It's easy to forget to call Dispose() on the Process instance.
  • If nothing else besides Id uses the Process instance, then the linked size grows unnecessarily by increasing the graph of types referenced.
  • It is somewhat difficult to discover or find this API.

System.Environment.ProcessId avoids all the above.

Example of incorrect code:

int pid = Process.GetCurrentProcess().Id;

Example of correct code:

int pid = System.Environment.ProcessId;

Further Reading