简体   繁体   中英

Hard .net Question

I want to share some question in which i got confused. Please check. :-)

How many GC threads do we have in a .NET process running on quad-core i7 Process?

I said 4x2=8?

Which GC generation utilize more memory and why?

I said Gen2 , but don't know why? i guessed because it's size is shown bigger in every book or net. :-P

Contravariance is the corollary of covariance? Explain.

Its from generics, but don't know how to explain it.

EnlistDistributedTransaction method is not supported by which database?

i said Oracle and IBM DB?

Importance of codeBase Element in C# and where it is use?

I said Assembly, but don't know the exact importance

Well , I managed to clear the interview, but these question really took me out. Please see if you can give me some advice on these question?

thanks Ajitpal

CoVariance/ContraVariance

For CoVariance/ContraVariance they were perhaps thinking of the example of Arrays (or in general of R/W CoVariant objects)

class A
{
}

class B : A
{
}

static void Func(A[] a) {
    a[0] = new A();
}

B[] b = new B[5];
Func(b);

This is "legal" to write, but it will throw on the assignment (in the Func) (ArrayTypeMismatchException). Here the CoVariant caused a problem. From the POV of Func it's "strange". I'll say that ContraVariance is a "missing" corollary of ReadWrite CoVariance (it should be there, but clearly it's impossible to do it)

For the GC

http://architecturebyashwani.blogspot.com/2010/02/foreground-gc-and-background-gc.html

It SEEMS that Workstation GC always happens in the thread that allocates the memory, so there isn't any "extra" thread for the GC. I'll add that this is version-dependant, so it could change from version to version of .NET (and Mono does it differently). With Server GC you have specialized threads for GC, one for each processor. The I7 Quad Core of the example probably has HT (HyperThreading), so 8 "cores", so 8 threads.

Generation Size

For the size of the generations... I would say that "normally" the Gen2 is larger, because large objects are always Gen2 ( http://msdn.microsoft.com/en-us/magazine/cc534993.aspx ) (technically they aren't Gen2... They live in a separate space that is checked when Gen2 is checked, but we will ignore this... the question wasn't very clear, and it is implementation-defined and opaque enough that we don't know EXACTLY how the Gens are "LinkedListed" in memory), and long lived objects will go to Gen2 (so after the "startup" phase of your program, the singletons and other long-lived objects are all in Gen2)

BUT ... in general this isn't true.

Let's say you make a program that does a single allocation ( var obj = new object() ). That allocation will start at Gen0. When that object is allocated, there is a single object, and that object is Gen0, so technically Gen0 is the biggest one.

DTC

IBM DB2 supports the DTC, and so Oracle:

It's semi-tautological, but I would say that EnlistDistributedTransaction is not supported by

  • DB that don't support transactions (some MySql, depending on the type of the base DB)
  • DB that don't support a transaction monitor/transaction coordinator, but from what I've read, newer MySql, using base DB that support transactions, seems to be able to support DTC (for example read http://dev.mysql.com/doc/refman/5.0/en/xa.html )

<codeBase> Element

Written THIS way it's much more clearer

http://msdn.microsoft.com/en-us/library/efs781xb.aspx

You can force your app to run with a particular version of a referenced assembly. Useful if there are "breaking changes" between versions of an assembly.

Covariance and Contravariance basically deals with the ability how to use less derived type than original specified type.

Covariance For use of more derived type than original specified.

Contravariance For use of more generic type (ie Less Derived type) than originally specified.

For more clarification visit below article https://docs.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance

  1. Implementation detail. Can differ in different versions of .net. And might differ between server/client GC. And might change depending on which phase the GC is in too. I assume it will be as many threads as there are (virtual) CPUs/Cores in at least some phases of the gc, so a 2x hyperthreaded quadocore might use up to 8 threads. But I wouldn't be surprised if some phases are just singlethreaded.
    In particular the client GC runs largely in parallel to your other code. So using all cores might be a bad idea because it slows the program down. Server GC on the other hand is optimized for maximum speed and does stop the whole program. So for Server GC multi-threading as much as possible sounds like a good idea.

  2. Most likely Gen2 because all long lived objects accumulate there. But this depends on your memory allocation pattern. The other generations have (soft) memory limits after which a collection for this generation occurs.
    And then there is the large object heap which is a heap separate from the normal generation heaps. If you allocate large objects there are good chances that it becomes bigger than the normal generation heaps. I think it is only collected during Gen2 collections, so one could count it as part of Gen2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM