Alex Rivera | Logout

.NET generation 0 heap size

Asked 2009-05-07T11:03:00.560
17

Is it possible to set a minimal size of a generation 0 heap in .NET?

I have a folowing sistuation. I have a function that allocates around 20-30 MB of 1KB objects, does something with them, and terminates, leaving all the allocated objects to be GC-ed. Now, in Performance Monitor, I can see that generation 0 heap size is 5-6 MB, which is not enough to accept all the 20-30 MB of objects that I need. When I start allocating, at some point gen0 GC starts running, and since all the objects are needed it promotes them to a gen1. Next time GC starts running, these objects get promoted in gen2. So finally around 15MB of my objects end up in the gen2 heap. These are, by my logic, temporary objects that should in no way end up in the gen2 heap. I belive the problem is in the size of the gen0 heap size. But I'm not sure. I know that in Java there is a possibility to set a minimal size of the generational heaps. Is there such a way in .NET?

Edit
Report

2 Answers

2

+1 to Andrew. The GC is self-tuning it learns about the app's memory need patterns on the fly.
In your case, if the GC performs a collection of Gen 0 and finds that a lot of objects survived / not a lot of memory was reclaimed, the garbage collector will grow the Gen 0 budget/quota automatically.

Looking at GC Type members, there doesn't seem to be a way to programatically configure GC Generation budgets/size.

answered 2009-05-07T11:16:42.400
2

At first generation0 may increase and decrease but these are in future. when process runs the initialized managed heap has fixed size in generation0 256 KB (which meant Richter) because modern computers have 256 and more memory for 2-nd level CPU's cash. Second. Andrew, you are right. When some object is so large it goes to gen2 at once. I can suppose you wanted to decrease gen0 size to provoke more frequently called GC to clear unused space and keep your app lighter. I have same problem in a SilverLight web site and search a solution. I think what if code creates much object which have roots and when gen0 is full CLR calls GC which move them to gen1, and then when objects grow more move them to gen2. Let's say all 3 generation are nearly full. Then for example GC called. What it will do. Just clear generation0 ? How with 1-st and 2-nd ones. I think sollution to keep memory light is the following. First create small and much objects instead of large in size and less in count. Second objects internal referencing must be one way oriented and not chaotic referencing to each other. Static objects which are not created dynamically for example according to comming records from database, need to keep in global variables and not create again when code cals them.

answered 2009-10-29T09:14:34.130

Your Answer