tags:

views:

428

answers:

4

I am trying to compile a one-off "script", an autogenerated C# program. This program contains 120,000 different string literals. The C# compiler can't build this, saying:

Unexpected error writing metadata to file '<removed>' -- 'No logical space left to create more user strings.'

Is there a hard limit in .NET on the number of string literals one can have in a module? What is this limit? Is there any way around it?

A: 

From MSDN :

Compiler Error CS0013: This error occures when your project builds a module with the same name as a module that is referenced by your project. That is, if your project creates a DLL named MyDll.DLL and your project also have a reference to a module with the same name (MyDll.DLL), then this error occures.


Also, there's a limitation on the size of generated assembly in Visual Studio.
Check the "BTS06 developers troubleshooting guide".

Andreas Grech
Yes, I saw that. This is not the problem.
romkyns
+7  A: 

I have no idea of what the limits are in .NET but, if it is a resource limitation, I'd solve it the same way we solved running out of space in the bad old 64K-segment days.

Externalize the strings - put them in an external file and simply store the offsets (and lengths if they're not null terminated). When you need a string, load it from the file and use it.

paxdiablo
What's wrong with this perfectly reasonable answer?
Nathan Taylor
Rather than just tackling the outcome, I would look into what the compiler error means first. The MSDN page given by Dreas and myself suggests that this could be a DLL conflict or a disk space problem rather than anything to do with fixed limits on the number of strings.
Simon P Stevens
Yes, but the phrase "contains 120,000 different string literals" seems to point to an actual resource problem rather than a naming problem. And I made sure I stated "if it is a resource limitation". I don't know for sure, just offering advice on how to get around it. I suspect that's at least as useful as someone parroting the first thing they found by entering the error into Google :-)
paxdiablo
+2  A: 

I've never encountered this before myself, but there is some information and suggested solutions on MSDN

However, unless you have a good reason for using hard coded string literals, you should probably consider using resource files rather than literals for most strings.

Simon P Stevens
Yes, I saw this. I doubt it's a broken install. The error message says quite clearly that there are too many strings, and indeed, 120,000 strings is a LOT of strings :)
romkyns
The page also suggests a problem with a conflict between dll names. (Check the community content at the bottom)
Simon P Stevens
+8  A: 

There is a limit on the number of strings in an assembly, just as there are limits on the number of classes, fields, etc. Each of these is identified with a 32-bit metadata token, where the upper-most byte is a meta-type-code, and the lower bits are the individual data record. For strings, they actually identify an offset into the string heap, so you can have at most 2**24 bytes for strings, i.e. 16MiB. Not sure whether strings are stored in UTF-8 or UTF-16.

Martin v. Löwis
The entire source file is 14 MiB in UTF-8, so if they are stored in UTF-16 the limit will be exceeded. Makes sense!
romkyns