classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String.equals optimisation


From: Archie Cobbs
Subject: Re: String.equals optimisation
Date: Tue, 12 Jul 2005 09:14:54 -0500
User-agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.3) Gecko/20041129

Simon Kitching wrote:
* Class.getName returns strings that have been interned. I don't
 think this is explicitly required by the java specs but is
 certainly true for Sun's JVM and seems likely to be done by
 any sensible JVM.

You definitely make some good arguments, but this one is not
neccesarily true. In fact, I'd argue a JVM that interns every
class' name (even if only on demand) is potentially wasting
a bunch of heap space.

I'm assuming that the Class object would contain a reference to the
interned string, so there is only one copy of the string, ie somewhere

Not a valid assumtion.. in JC no String is associated with Class
objects.  VMClass.getName() is native and the returned String is
created on demand, based on the UTF-8 name stored internally in memory.

In fact, one could argue that storing class names in any other way
than in their native UTF-8 form is a big waste of memory. E.g.,
for loaded classes...

If the VM can find it's UTF-8 name and create a String dynamically:
  Then also storing the class name persistently as a String is a
  200% increase in memory (a char[] array is twice as big as UTF-8)
Else:
  The VM must store the class name as a String, which is a 100%
  increase in memory vs. storing it as UTF-8

The extra space used for interning is therefore just a single extra
reference (as a reference to the string is contained in both the Class
object and the String class internal pool). Yes that is a little space
wasted, but not a bunch.

Right, the wasted space is not much.. at first I was forgetting
that intern'd strings are stored with weak keys and will get
flushed out after they're no longer referenced (just like normal
Strings)... replace "big waste of memory" with "waste of memory" :-)

-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]