Re: Prevayler-Discussion Digest, Vol 34, Issue 1

"Justin T. Sampson" <[email protected]>
Newsgroups gmane.comp.java.prevayler
Message-ID <[email protected]>
Howdy,

I haven't needed this kind of thing myself, though I've heard of it
outside of the Prevayler context. I don't think it would be any more
common for Prevayler-based systems than for Java-based systems in
general.

I agree that String.intern is probably the wrong solution, since it
uses permgen space (as Angelo mentioned) and introduces some thread
synchronization overhead. The permgen space does get garbage-collected
occasionally, but it's much smaller than the full heap. (It's where
classes are loaded as well.)

One challenge to keep in mind when rolling your own is getting
threadsafety right. It's also worth remembering that you really only
need a heuristic solution -- as long as your code doesn't *depend* on
the identity of string instances, all you care about is *reducing* the
duplication, not necessarily *eliminating* it.

More below.

On Sun, Aug 15, 2010 at 10:30 AM, Karl Wettin <[email protected]> wrote:
> 15 aug 2010 kl. 18.23 skrev Klaus Wuestefeld:
> >> I was simply
> >> thinking a Map<String, String> where key and value is the same
> >> instance.
> >
> > That is the same as a Set. You propably want a
> > Set<WeakReference<String>>.

Not quite, for two reasons:
1. Set doesn't let you retrieve the existing value.
2. WeakReference doesn't delegate to the contained object's equals/hashCode.

If you want weak references as keys, you have to use WeakHashMap. But
also remember to store the values as WeakReferences as well, otherwise
they'd never be garbage because the map itself refers to them.
Alternatively, look at MapMaker from the Google Guava library, which
makes this all more convenient, including producing a concurrent
(threadsafe) map.

http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/MapMaker.html

(I think I even ran across a blog post somewhere describing how to use
MapMaker for exactly this use case.)

> I'm not sure about the WeakReference as there is a cost associated
> with that too. Iterating all my instances once a week to find the
> delta it probably enough for me as I pretty much never delete
> anything. Or is there something I'm forgetting?

WeakHashMap does indeed introduce overhead -- there's some surprising
thread synchronization going on. You probably don't need the weakness
if the values do tend to be reused a lot.

> > And Strings use 2 bytes for each char. If you have mostly ASCII, you
> > can use byte[] instead to store String.getBytes("UTF-8").
>
> Another good point.
>
> Perhaps I should implement my own String, using the first byte as
> bitset for encoding and what not. I'll report back later with any
> findings.

Raw byte[] would be dangerous since it's mutable, but if you wrap it
in your own class that's less of an issue. On the other hand, it
sounds like your main issue is simply the duplication of strings, so
solving that problem will reduce your memory consumption dramatically
without bothering to optimize the size of individual strings.

Cheers,
Justin

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
To unsubscribe go to the end of this page: http://lists.sourceforge.net/lists/listinfo/prevayler-discussion
_______________________________________________
"Databases in Memoriam" -- http://www.prevayler.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.