TAGS :Viewed: 4 - Published at: a few seconds ago

[ Process.Start enforces additional memory constraints on a process? ]

I have written a PowerShell cmdlet, which takes away some memory.

protected override void ProcessRecord()
    Marshal.AllocHGlobal(Size * 1024 * 1024);

It works fine, if it runs in a PowerShell prompt directly.

However, if I try to start a process of PowerShell in C# and run it, it throws an OutOfMemoryException.

Process.Start("PowerShell", @"-NoExit -Command ""Import-Module Something.dll; Take-Memory -Size 1000;""");

Does Process.Start enforce additional memory constraints on a process? How can I work around with this?


Using Run with

PowerShell -NoExit -Command "Import-Module Something.dll; Take-Memory -Size 1000;"

also works fine.

Answer 1

Not sure, I try an answer here because it's too long for a comment :

Hypothesis :

Your are using a 64 bits OS (Seven, Windows 8 ...)

When you run PowerShell command line or run PowerShell at the command line, by default you run 64 bits PowerShell.

When you compile your C# program, you don't take care of the fact that you target a 32 bits executable. So your EXE by default invoke a 32 bits PowerShell exe. In my opinion 1Go is too big to be found in one piece in a 32 bits PowerShell EXE.

Conclusion :

Can you try to target 64 bits for your C# EXE program ?