CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Greg Young [MVP]


Pop Quiz: Pinning

OK you guys are good. I imagine that the average codebetter.com reader is outside the high side first standard deviation of the average .NET developers ...

And yes I guess I am in a way stealing this idea from Rico 

I warn you this is a tough one. 

So lets start, answers will be posted tomorrow or Saturday if I still see a lot of responses.

Pop Quiz:

We know what pinning is (it keeps the garbage collector from moving an object in memory during a collection as something may have its address that the GC can't control). All questions apply to the current 2.0 release of the CLR on an x86 platform.

 

Question 1:

What are the various ways that an object can be pinned?

Question 2:

What is heap fragmentation and why is it bad?

Question 3: 

When is heap fragmentation not important?

Question 4:

How can you tell if an object is pinned?

Question 5:

Can a pinned object be promoted?

 

Leave answers in comments unless you know for a FACT you are right on them all ... then email them to me at gregoryyoung1@ that google email service and I will use them tomorrow.



Comments

JD Conley said:

1. GCHandle, P/Invoke & InteropServices automagically pins things, c# fixed statement, and probably more I don't know. 2. Unallocated blocks of memory in the heap that the .NET memory manager likely won't re-use. That means your app ends up using more virtual memory than it actually needs. In x86 land, with a lot of memory thrash, this can quickly lead to out of memory situations. 3. If there are big enough holes that the free space gets re-used by the memory manager (.net 2.0 is a lot smarter about this). If your objects are unpinned quickly and the space is reclaimed. 4. It's hard. Windbg + SOS. !DumpHeap and comb through to see the objects below large chunks of free space. At least, that's the easiest way I've seen. 5. No? (not sure on this one..) Let the flogging begin! ;)
# August 9, 2007 7:20 PM

Peter Ritchie said:

1. GCHandle, C#: fixed, C++ pin_ptr, CLR interop effectively pins for duration of a "call", and GC.KeepAlive. 2. heap fragmentation is when the gap between memory blocks increases in size and frequency. This is bad because the sum of the free blocks may amount to a large sum, but the largest block than can be allocated is the largest free block (not the sum) increasing the likelihood of OOM... 3. I don't think heap fragmentation is not important; but fragmentation in the large block heap could be considered less important. 4. You can tell if an object is pinned because you haven't freed it's pinned GCHandle :-) 5. No, a pinned object cannot be promoted.
# August 9, 2007 9:53 PM

Jason Haley said:

# August 10, 2007 10:19 AM

Steve Johnson said:

I just have a theoretical comment on #5. It is possible that an object could be promoted even though pinned. Gen0 and Gen1 are located in the ephemeral segment (1 per heap). Gen2 can be located in the ephemeral segment, plus any number of non-ephemeral segments. If a Gen0/Gen1 object were pinned, there are two possibilities under which I could see it being promoted w/o being moved. 1 - The pinned object is in Gen0 and the GC moves the Gen0 boundary up, leaving the pinned object in Gen1. 2 - A new ephemeral segment is allocated by the GC, leaving the pinned object in the old (now Gen2) segment. As I said, this is merely a theoretical comment. I'm not sure this actually happens, but I don't see why it couldn't, or even why it wouldn't.
# August 10, 2007 10:28 AM

Craig Carpenter said:

Keep in mind..I'm a monkey and not at the same level of knowledge of the CLR than the rest of the readers of this blog.  But here goes.

1.  Dunno.  My dumb monkey response is "hang on to the reference", but I bet that's to vague for the consumers of this blog.  The first thing that comes to mind is GC.KeepAlive.

2.  Um...it's fragmentation that occurs in the heap. *grin*  It happens when there are unused or unclaimed bits of space in the heap, but there's nothing the CLR can or will fit in the spaces.  It's bad because once you run out of memory on a Windows machine, you're going to start swapping.

3.  I'd say heap fragmentation is important for some incredibly low number of Windows apps...like less than 5%.  The rest of those apps are general business apps and as long as you've got a huge chunk of memory (like most machines in the last few years do), the performance will be just fine.  Plus, GC actually works in .NET, so it's typically not a problem.

4.  Dunno.

5.  I'm sure if I knew what "promoted' meant in this context, I'd have something useful to say.

# August 10, 2007 3:11 PM

Peter Ritchie said:

Maybe I'm wrong; but I though each generation was a specific segment of memory?  If that's the case, and a pinned object can't change memory locations, it cannot change to another segment and therefore can't be promoted to new generation.

# August 10, 2007 3:27 PM

That guy in Russia said:

1. Explicit, Interop, PInvoke. 2. HF is when mem is allocated asymmetrically, resulting in sub optimal allocation with alloc performance degrading in the neighborhood of O(n^2), depending on implementation. 3. HF not important for processes that will not repeatedly alloc/dealloc, will alloc little in relation to H size, or will alloc objects of the same size. 4. You can look at the handle table for the GC (GCHandleLeaks in SOS), or if you are lucky you can read the code. 5. Of course it can be promoted, it just can't be freed.
# August 11, 2007 9:30 AM

Steve Johnson said:

Gen0 and Gen1, the ephemeral generations, are always located in a single segment, per heap, called the "ephemeral segment". Part of Gen2 is also usually located at the beginning of the ephemeral segment. Each heap can have any number of additional segments.
# August 13, 2007 10:41 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!