Find the bugThe concurrent memory buster
This is test code, to check something out, and it blew up in my face after consuming all the memory on the machine.
Can you see why?
Parallel.For(0, 10, i => { var list = new List<int>(); int c = 0; int sum = 0; for (int x = 0; x < 10000; i++) { if (c != list.Capacity) { c = list.Capacity; sum += c; } list.Add(sum); } });
More posts in "Find the bug" series:
- (29 Feb 2016) When you can't rely on your own identity
- (05 Jan 2016) The case of the degrading system–Answer
- (04 Jan 2016) The case of the degrading system
- (11 Sep 2015) The concurrent memory buster
- (20 Apr 2011) Why do I get a Null Reference Exception?
- (25 Nov 2010) A broken tree
- (13 Aug 2010) RavenDB HiLo implementation
- (25 Jul 2010) Accidental code reviews
Comments
You created an endless loop by not incrementing x: for (int x = 0; x < 10000; i++)
[root cause analysis] You were probably staying up too late and didn't have enough fresh coffee on-hand.
If c is greater than list of capacity ?
Because List doubles its capacity when it that is reached, plus memory fragmentation of disposing all the intermediate internal arrays ?
Sad i++. :( I'm gutted R#er didn't alert me about it.
You're incrementing "i" instead of "x" in your for loop. X will always be zero so you have 10 infinite loops running in parallel == OOM exception
Parallel infinite loops. i++ should be x++.
Infinite loop due to incrementing i instead of x. Well, not just one infinite loop, 10 of them.
Well the memory blow up due to the list add otherwise it would only consume 100% cpu
Cognitive bias, more precisely, Selective Perception (https://en.wikipedia.org/wiki/Selective_perception)
You've read&written 'for (int x = 0; x < 10000; x++)' soooo many times in your career, that your brain actually SEES 'x++' instead of 'i++'.
And the reason why programmer's keyboards should deliver electric shocks when 'Ctrl-C+Ctrl-V'-ing. You can't help it... it is the way human brain works. So, you better stay away.
Comment preview