Friday, October 23, 2009

How many classes can a single .NET DLL contain?(VS2010 + Parallel)


I just tried the last version of MS VS 2010 beta2 with my little "killer" application(see 2 entries in the past...). The only difference is that I used the Parallel.For method instead of a classic for.
I was happy (really, I was happy! ) that I could find a possible memory leak inside it because the memory used by VS2010 was increasing in a linearly fashion. Therefore, I've decided to post a comment on Connect website to notify MS team to fix this issue.
You can find the entry here.

The best part is that I tried to print screen to get the image of the task manager but it couldn't store it in the clipboard. Pretty cool, right? :D

Happy coding...

Tuesday, October 20, 2009

How many classes a single .NET DLL contain?(Log)

On a Toshiba CoreDuo @2GHz and 2 GB RAM:
@10:30 PM: 0 types.
@2:00 AM: 263.700 types and counting...
@8:52 AM: 419.300 types and counting...
@6:22 PM: 562.400 types and counting (very slow) and the memory is at ~1 GB...

I don't think I'll find out if an assembly can "support" more than 2^24 types inside it... :)

If any of you can create this kind of assembly, please let me know... :)

Happy coding!

Monday, October 19, 2009

How many classes can a single .NET DLL contain?(2)

Hi there... As I said in my early post, I will try to create an application that generates an assembly that has more than 2^24– 1 or 16.777.215 types. So, here's the code:

After 3 hours on a IBM ThinkCentre machine @2GHz with HT and 2GB of RAM, I was able to create only aprox. 200.000 types. I try again on my personal computer and let it run for the whole night... :)

Here's a link to the application.

Have fun... :)

See you with new results late on... :)

Saturday, October 3, 2009

How many classes can a single .NET DLL contain?

Hello all…

There’s a common question in a programming interview that sounds like this:

“How many classes can a single .NET DLL contain?”

I did a little research on the web and I saw that lots of people think that the number is unlimited. Well, in my opinion, this is wrong!

Let me clarify this! Inside MSDN there’s a section on Overview on .NET Framework that contains a section on Metadata and Self-Describing Components. In this section you will find out how metadata is stored inside a Portable Executable and what is the structure of Metadata. When you are looking inside “Metadata and the PE File Structure” , you will find out that each element of your program is stored in a “Metadata table”(one for classes, one for members, a.s.o.). Each element is identified by a “Metadata token” and it’s definition is: ”Each row of each metadata table is uniquely identified in the MSIL portion of the PE file by a metadata token.Metadata tokens are conceptually similar to pointers, persisted in MSIL, that reference a particular metadata table.”(see MSDN). After this section, there is an explanation of these tokens. “A metadata token is a four-byte number. The top byte denotes the metadata table to which a particular token refers (method, type, and so on). The remaining three bytes specify the row in the metadata table that corresponds to the programming element being described.

Let me stop here. If a class is identified in the metadata table using a metadata token and the metadata token is a four-byte number, this means that the number of classes that an assembly can contain is smaller than the biggest number on 3 bytes (the first byte is a connection to the metadata table type). The biggest number on 3 bytes is 2^24– 1 or 16777215.

Therefore, the maximum number of classes (and classes members) that a .NET assembly can contain is 16777215. Am I right? (Wait for comments!)

That’s nice, right? :)

If I’ll have the time, I’ll try to write some code that will generate an assembly with more than 16777215 classes and see if I can use it… :)

Happy coding…

P.S.: Next time when I’ll ask this question on an interview, I’ll be careful on the answer!