I found one unpleasant behavior developing with Visual Studio. It was hanging my machine while compiling C#.
I have reduced behavior to the next minimal source code
using System.Collections.Generic;
using System.Linq;
namespace memoryOverflowCsharpCompiler {
class SomeType { public decimal x; }
class TypeWrapper : Dictionary<int,
Dictionary<int,
Dictionary<int, SomeType [] []>>> {
public decimal minimumX() {
return base.Values.Min(a =>
a.Values.Min(b =>
b.Values.Min(c =>
c .Sum(d =>
d .Sum(e => e.x)))));
}
}
}
Compiling with
PROMPT> csc source.cs
*** BANG! overflow memory usage (up to ~3G)
PROMPT> csc /?
Microsoft (R) Visual C# Compiler version 12.0.30501.0
Copyright (C) Microsoft Corporation. All rights reserved.
...
(using Windows 8.1 Pro N x64; csc
compiler process is running with 32bits)
sightly modifications don't produce this behavior (eg. changing decimal
by int
, reducing one nested level, ...), performing a big Select
then reducing, works fine
Explicit workaround:
return base.Values.SelectMany(a =>
a.Values.SelectMany(b =>
b.Values.Select (c =>
c. Sum (d =>
d. Sum (e => e.x))))).Min();
Although this explicit workaround exists, it not guaranteed that this behavior will not occur again.
What's wrong?
Thank you!