4

Was wondering if anyone could shed some light on this.

I have an application which has a large memory footprint (& memory churn). There aren't any memory leaks and GCs tend to do a good job of freeing up resources.

Occasionally, however, a GC does not happen 'on time', causing an out of memory exception. I was wondering if anyone could shed any light on this?

I've used the REDGate profiler, which is very good - the application has a typical 'sawtooth' pattern - the OOMs happen at the top of the sawtooth. Unfortunately the profiler can't be used (AFAIK) to identify sources of memory churn.

Is it possible to set a memory 'soft limit', at which a GC should be forced? At the moment, a GC is only performed when the memory is at its absolute limit, resulting in OOMs.

trilson86
  • 939
  • 1
  • 9
  • 20
  • have you used http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/ to verify that there are no leaks? – JP Hellemons Apr 10 '13 at 10:18
  • is it not possible to wrap big parts in usings or invoke GC.Collect occasionally? – slawekwin Apr 10 '13 at 10:20
  • You can force the GC to run. check out these [SO post](http://stackoverflow.com/questions/4257372/how-to-force-garbage-collector-to-run), [another SO post](http://stackoverflow.com/questions/233596/best-practice-for-forcing-garbage-collection-in-c-sharp) – scheien Apr 10 '13 at 10:20
  • 1
    Curious as to why there's so much data required in-memory at once.. – Simon Whitehead Apr 10 '13 at 10:22
  • Without any knowledge of you application. Is it possible that you are close to the limit of memory then you deallocate and immediately re-allocate some memory? If so, can you force a garbage collection before you re-allocate see if that helps? – TheKingDave Apr 10 '13 at 10:24
  • 1
    I would run a memory profiler cause more often than not you are doing something that you don't want to do, when you get an out of memory exception. Calling garbage collector manually will not help the situation - if it did, then you wouldn't get the OOM exception in the first place. – lahsrah Apr 10 '13 at 10:27
  • Stop allocating and collecting, and instead consider using a pool type architecture instead. This reduces memory thrashing in a world of indeterminate GC, – spender Apr 10 '13 at 10:34
  • 1
    "Tere aren't any memory leaks" isn't that a bit to optimistic? – dowhilefor Apr 10 '13 at 10:34
  • I've used the REDGate profiler, which is very good - the application has a typical 'sawtooth' pattern - the OOMs happen at the top of the sawtooth. Unfortunately the profiler can't be used to identify sources of memory churn – trilson86 Apr 10 '13 at 10:37
  • If you're using any collections, consider providing a capacity for them when you create them to avoid the thrashing associated with growing the collection. – spender Apr 10 '13 at 10:37
  • Do you create many large (>85KB) objects? – Henrik Jul 26 '13 at 13:17

3 Answers3

2

It shouldn't really be possible for a Garbage Collection to 'not to happen in time'. They happen when a new memory allocation would push Gen-0 past a certain limit. Thus they always happen before a memory allocation would push the memory past its limit. This happens so many times a day throughout the world I would be surprised if any bugs weren't well known about.

Have you considered that you might actually be allocating more memory than is available? The OS only lets you access 2GB on most 32-bit machines.

There are some other possibilities:

  1. Is your application using un-managed memory?
  2. Is your application Pinning any memory? If so that could cause a fragmentation issue especially if you aren't releasing pin.
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
  • The question seems to be asking if the limit is settable. Could you add any info as to how the Gen-0 limit is determined. For instance does it take into account the overall free system memory. – crokusek Feb 13 '15 at 18:45
1

If you use a lot of memory and you garbage collect a lot I guess you should consider the "Flyweight" design pattern.

As an example, if you garbage collect a lot of strings, see String.Intern(string s). Msdn reference

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
Thomas
  • 5,603
  • 5
  • 32
  • 48
  • 1
    What is the "lightweight" design pattern? – spender Apr 10 '13 at 10:36
  • I'm curious now too. Do you have a source for this design patter @Thomas? – Simon Whitehead Apr 10 '13 at 10:42
  • Originally @Thomas wrote `lightweight design pattern`, perhaps he meant Flyweight pattern or smth like that, but while making some grammar fixes I've wrapped `lighweight` word into braces and that gave us `"lightweight"` which confused us. Sorry for that. – alex.b Apr 10 '13 at 11:56
  • Thanks Aleksey, I was thinking in French (patron de conception Poids-mouche - http://fr.wikipedia.org/wiki/Poids-mouche_(patron_de_conception) ), sorry for my bad traduction. I meant Flyweight. – Thomas Apr 10 '13 at 12:25
  • I changed the wording to Flyewight for you Thomas. – Martin Brown Mar 24 '15 at 17:53
-1

You can use GC.collect() to force the garbage collector to do its work. But it is not preferable.

Use memory profiles like(memprofiler) to detect the leaks. Almost all your code performs leaks at some points.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Jo9876
  • 13