[Mono-list] How many strings do I have?

Paolo Molaro lupus@ximian.com
Mon, 11 Oct 2004 17:56:23 +0200


On 10/09/04 Jonathan Pryor wrote:
> On Fri, 2004-10-08 at 04:39, Jeroen Frijters wrote:
> > Jonathan Gilbert wrote:
> > > Once a string is in the pool, it can NEVER be removed
> > > (until the AppDomain is disposed).
> > 
> > In Java the string intern pool uses weak references. I don't know about
> > .NET, but I'd hope it does the same.
> 
> I believe it doesn't.  From [1]:
> 
>         Interning happens two ways in the CLR.
>         ... 
>         
>         2) It happens automatically, when you load an assembly.  All the
>         string literals in the assembly are intern???ed.  This is

I doubt this is true, though I'm lazy and didn't write a test.
What the current C# compiler behaviour imposes is to intern all the strings 
referenced by a method before the method is executed, so AFAIK, this is
a requirement per-method and not per-assembly.
I'm not sure why using weak references would help: the strings must continue
to exist until the appdomain is shut down anyway.

As for the original issue: mono behaves that way because it uses the
unmanaged representation of a string as the key in the hash of interned
strings. This made sense at the time when the code was written (we had 
just the interpreter). The code should be changed to use MonoString* as 
the key since this will make the string intern icalls faster.

lupus

-- 
-----------------------------------------------------------------
lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better