0

I see the following pattern used in the code a lot. Does this cause some form of Memory leak when used for large number of strings and string concatenation operations (millions of operations on strings of varying sizes).

pattern = (new CommonPattern(form)).ToString();

The class implementing the CommonPattern looks like this (after boiling down to code relevant to the question):

internal class CommonPattern {
    private string pattern;
    private TForm form;

    public CommonPattern(TForm form) {
        pattern = pattern + form.Name;
    }

    public override string ToString() {
        return pattern;
    }
}

I have tried adding a destructor like so.

~CommonDesignerPattern() { }

When calling this code in small projects we are not seeing any issues. However in projects with 150 – 200 forms we are seeing significant issue – leading to Out of memory Exception.

A memory profiler has not helped me in finding this issue. When I did the analysis, it pointed to String and byte[] as the final suspect, which did not help me.

I work on a small part of a very big project and wanted to learn if the call to new like it is used in the above code causes memory leaks and learn how to tackle this if it was a culprit.

Lot of search results online point to EventHandlers. But specifically I want to know if the case above can lead to a memory leak.

  • 1
    How long are the strings you are repeatedly constructing? Are they longer than 42,500 characters? Strings that large are going to be stored on the [large object heap](http://stackoverflow.com/q/8951836/3744182). Allocating many strings that size can cause memory fragmentation. – dbc Dec 31 '16 at 05:49
  • If everything string related is done in such a manner, and there are literally millions of such operations, then I'd recommend to look into http://stackoverflow.com/questions/21644658/how-to-use-stringbuilder-wisely. In most cases there is no need to [optimize it](https://blog.codinghorror.com/the-sad-tragedy-of-micro-optimization-theater/), but in this case that shouldn't probably be overlooked. Nonetheless, as it has been already mentioned, such construct(`new T().ToString()`) shouldn't cause memory leaks. – Eugene Podskal Jan 01 '17 at 16:27
  • the problem i am facing takes a long time to reproduce, so I am trying out different options. Thanks for the links to informative content online. – Ganesh Kamath - 'Code Frenzy' Jan 02 '17 at 06:25

1 Answers1

0

There is no memory leak in the class code you posted. String concatenation does create additional instances of strings, however, once those strings are out of scope, they will eventually be cleaned up by the garbage collector.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Hi John. Does the memory get reclaimed at the close of the application or at the end of the function where the **pattern = (new CommonPattern(form)).ToString();** statement is made? – Ganesh Kamath - 'Code Frenzy' Dec 31 '16 at 05:14
  • The memory will get reclaimed when the garbage collector runs. The GC will run when there is memory pressure to do so. See this for more info: https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx#conditions_for_a_garbage_collection – John Koerner Dec 31 '16 at 05:17
  • Hi John Koerner, can you recommend me some alternative implementation that causes a quick memory freeing. – Ganesh Kamath - 'Code Frenzy' Dec 31 '16 at 05:34
  • If there is memory pressure then the memory will be freed rather quickly. I have a sample app that I use to demonstrate the VS 2015 diagnostics tools and once it starts hitting a high amount of memory usage, it will start calling the GC quite often to try and free up memory. See more on my blog: https://johnkoerner.com/csharp/analyzing-memory-usage-in-visual-studio-2015/ – John Koerner Dec 31 '16 at 05:42
  • 1
    I am developing in VS 2010. Let me try the code on VS 2015 and get back. Nice read by the way. Very well written. – Ganesh Kamath - 'Code Frenzy' Dec 31 '16 at 06:06
  • @JohnKoerner I would like to see that sample app. I have lots of trouble with the GC it just does not works well at all an often gave me out of memory exceptions. In each case there was no memory leak, just lack of GC doing its job and obsessively collecting weak references – GuidoG Dec 31 '16 at 09:00
  • I ran a debug session and got the following result: https://drive.google.com/open?id=0B4zUWFlcSqrXUDJUSUo0eFMwUGM – Ganesh Kamath - 'Code Frenzy' Dec 31 '16 at 09:42
  • hi @JohnKoerner, thank you so much for your guidance. I ran the .Net Memory Profiler (7 day trial version) and made suggested fixes from StackOverflow now finally my application seems to free up a huge chunk of memory after completing of each activity. It took me all these days (17 days) to make changes to all my modules seems like it was well worth the effort. – Ganesh Kamath - 'Code Frenzy' Jan 16 '17 at 13:00