Groups | Blog | Home
all groups > dotnet clr > july 2004 >

dotnet clr : Question on Memory Allocation


John Linn
7/12/2004 4:44:01 PM
Let's say I have this object:

public class void MyTest() {
public string str1 = [1k worth of text];
public string str2 = [1k worth of text];
public string str3 = [1k worth of text];
public string str4 = [1k worth of text];
public string str5 = [1k worth of text];
}

MyTest tmpTest = new MyTest();

..NET requires ~5k of continuous free memory to allocate this object, correct? The object instance and the string instances are stored together?

Side question: In CLRProfiler, will this object show up as one 5kb object, or will it show as 6 distinct objects?

Now, let's say I do this:
string myVar = tmpTest.str3;
tmpTest = null;
<---------- lets say under load, a GC happened here.
ProcessString(myVar);

Does the tmpTest instance and the 4 other strings get cleaned-up? Does the re-assigned string get promoted to gen 1? And if another GC happened before myVar got marked for removable, would it slide into Gen2?

Still trying to get my brain around this stuff.. =)

AlexS
7/12/2004 10:42:45 PM
John,

don't tease your brain - tease CLR Profiler and your app.
Profiler shows every single allocation. Unfortunately I cannot check the
code and guarantee if this is really so, however my practice is that if
something is allocated - even 1byte string - it could be found in CLR
Profiler data. If profiler doesn't crash...

Also, it seems profiler influences heavily GC behavior - freed memory is not
collected as quickly as in non-profiled application.

About your Q: it will be 5(6) different allocations until cleaned out by GC.
Depending on how long is delay between myVar assignment and ProcessString -
it might survive several GC cycles - all gens. tmpTest instance and 4 other
strings will be freed when GC kicks in - however when this will happen
nobody can say - use your app and profiler.

Another point: it doesn't make much point to tease your brain over such
examples. In real applications behavior is sometimes very different from
what you see in simple code snippets. Take good note of info in
http://msdn.microsoft.com/architecture/default.aspx?pull=/library/en-us/dnpag/html/scalenet.asp


HTH
Alex

[quoted text, click to view]
re-assigned string get promoted to gen 1? And if another GC happened before
myVar got marked for removable, would it slide into Gen2?
[quoted text, click to view]

Mattias Sjögren
7/13/2004 1:15:52 PM
John,

[quoted text, click to view]

If the [1k worth of text] is a string literal, it will be interned and
each MyTest object will get a reference to the same string instance.
So each additional MyTest object would only take around 30 bytes on a
32-bit system.


[quoted text, click to view]

Not necessarily.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Michael Giagnocavo [MVP]
7/13/2004 2:29:03 PM
[quoted text, click to view]

Just to make an obvious clarification: only if it's the same literal. :)

-mike
MVP


AddThis Social Bookmark Button