ChallengeThe loop that leaks
Consider the following code:
Can you tell me what the output will be? And why?
More posts in "Challenge" series:
- (01 Jul 2024) Efficient snapshotable state
- (13 Oct 2023) Fastest node selection metastable error state–answer
- (12 Oct 2023) Fastest node selection metastable error state
- (19 Sep 2023) Spot the bug
- (04 Jan 2023) what does this code print?
- (14 Dec 2022) What does this code print?
- (01 Jul 2022) Find the stack smash bug… – answer
- (30 Jun 2022) Find the stack smash bug…
- (03 Jun 2022) Spot the data corruption
- (06 May 2022) Spot the optimization–solution
- (05 May 2022) Spot the optimization
- (06 Apr 2022) Why is this code broken?
- (16 Dec 2021) Find the slow down–answer
- (15 Dec 2021) Find the slow down
- (03 Nov 2021) The code review bug that gives me nightmares–The fix
- (02 Nov 2021) The code review bug that gives me nightmares–the issue
- (01 Nov 2021) The code review bug that gives me nightmares
- (16 Jun 2021) Detecting livelihood in a distributed cluster
- (21 Apr 2020) Generate matching shard id–answer
- (20 Apr 2020) Generate matching shard id
- (02 Jan 2020) Spot the bug in the stream
- (28 Sep 2018) The loop that leaks–Answer
- (27 Sep 2018) The loop that leaks
- (03 Apr 2018) The invisible concurrency bug–Answer
- (02 Apr 2018) The invisible concurrency bug
- (31 Jan 2018) Find the bug in the fix–answer
- (30 Jan 2018) Find the bug in the fix
- (19 Jan 2017) What does this code do?
- (26 Jul 2016) The race condition in the TCP stack, answer
- (25 Jul 2016) The race condition in the TCP stack
- (28 Apr 2015) What is the meaning of this change?
- (26 Sep 2013) Spot the bug
- (27 May 2013) The problem of locking down tasks…
- (17 Oct 2011) Minimum number of round trips
- (23 Aug 2011) Recent Comments with Future Posts
- (02 Aug 2011) Modifying execution approaches
- (29 Apr 2011) Stop the leaks
- (23 Dec 2010) This code should never hit production
- (17 Dec 2010) Your own ThreadLocal
- (03 Dec 2010) Querying relative information with RavenDB
- (29 Jun 2010) Find the bug
- (23 Jun 2010) Dynamically dynamic
- (28 Apr 2010) What killed the application?
- (19 Mar 2010) What does this code do?
- (04 Mar 2010) Robust enumeration over external code
- (16 Feb 2010) Premature optimization, and all of that…
- (12 Feb 2010) Efficient querying
- (10 Feb 2010) Find the resource leak
- (21 Oct 2009) Can you spot the bug?
- (18 Oct 2009) Why is this wrong?
- (17 Oct 2009) Write the check in comment
- (15 Sep 2009) NH Prof Exporting Reports
- (02 Sep 2009) The lazy loaded inheritance many to one association OR/M conundrum
- (01 Sep 2009) Why isn’t select broken?
- (06 Aug 2009) Find the bug fixes
- (26 May 2009) Find the bug
- (14 May 2009) multi threaded test failure
- (11 May 2009) The regex that doesn’t match
- (24 Mar 2009) probability based selection
- (13 Mar 2009) C# Rewriting
- (18 Feb 2009) write a self extracting program
- (04 Sep 2008) Don't stop with the first DSL abstraction
- (02 Aug 2008) What is the problem?
- (28 Jul 2008) What does this code do?
- (26 Jul 2008) Find the bug fix
- (05 Jul 2008) Find the deadlock
- (03 Jul 2008) Find the bug
- (02 Jul 2008) What is wrong with this code
- (05 Jun 2008) why did the tests fail?
- (27 May 2008) Striving for better syntax
- (13 Apr 2008) calling generics without the generic type
- (12 Apr 2008) The directory tree
- (24 Mar 2008) Find the version
- (21 Jan 2008) Strongly typing weakly typed code
- (28 Jun 2007) Windsor Null Object Dependency Facility
Comments
On which framework? In debug or release? I assume it's supposed to leak, but I can't see it. In debug, the 128MB array is apparently not released immediately, but it is released at the next iteration, so that's not really a leak.
@Thomas, There is leak I believe. Spin that inside LINQPad without read line, I can see memory increase from every iteration.
It increase very steadily.
To make my observation more obvious . I run it for 5 min. It increase from 1378756 to 1471260. There are occasion where from execution to execution, increase is 0, sometime -50 to -150. Overall, it is constantly increase.
When increase is 0, there are 2~4 same numbers in a row. Then increase again.
1024 * 1024 * 128 bytes is a large object, so it will go to LOH, so memory consumption will grow unless LOH is compacted
Running the code on a .net Framework 4.7 ConsoleApp didn't show the same numbers or increase in memory consuption.
Without executing the code it should keep a 128MB array around in debug builds because stack local variables are not collected until the method is exited. For Release builds it should not leak though. Although you null out the reference to the array the MD5 method call can get the reference as stack local variable which will not be nulled out and lead the GC to believe that the array is still referenced. On a second thought this might also cause funny issues in release builds depending on how the JIT compiiler decides to pass parameters to managed methods.
MD5 needs to be disposed.
Looks like the first time you write to the console there are some allocations. Here is the same code with a writeline at the start, run it in debug mode and you will get constant results. https://gist.github.com/slang25/39acdb0f404bad691e72f450d9266885
^ I mean release mode sorry, not debug
@Jason the leak might caused by LinqPad. In a real program, I'm not seeing any increase after the 3rd iteration
Thomas, In debug, yes. The key here is that the value is being nulled, but not release.
Valery, Not really, we aren't holding to the value over a long period of time. It should recognize that this is free and reuse the space
Alois, That is indeed the issue, and surprising because the is no actual need to create a temporary stack variable. The scary thing on our part is that this is technically possible on release as well, yes.
Comment preview