Storing String Reference in a Map

Hi all:

Don't know if this is possible but I thought I would throw it out there.

Here's what I'm trying to do. I have a map which stores a String as a key and an ArrayList of Strings as a value. Then I have another map with the same structure. However, I'm having to store the same String objects in both maps which after many records begins to use a lot of memory. I could effectively cut the memory usage close to half if I could store a reference to the same String in the other map. Does this make sense?

Thanks,

John

[556 byte] By [johndsmitha] at [2007-10-3 1:02:59]
# 1

map1.put(string1.intern(), list1);

map2.put(string2.intern(), lis2);

Look up the docs for intern. It's roughly, "look in the pool for this string, and if it's there, return a reference to it, else add it and return a reference."

If string1 and string2 are string literals--that is, in your code, rather than variables, you're doing put("abc", ....), then this is already being done for you.

Likewise, you could intern each of the strings in the lists.

However, when you say, "begins to use a lot of memory," do you have numbers--have you measured it? Or are you just guessing that it will be "a lot"?

jverda at 2007-7-14 17:59:09 > top of Java-index,Core,Core APIs...
# 2
Oh, also, whenever you store anything in a map or list, you're always only storing the reference in the map or list, never the object.
jverda at 2007-7-14 17:59:09 > top of Java-index,Core,Core APIs...
# 3

Thanks that worked surprisingly well. It was interesting in how I could only apply the intern() method on one of my mapping keys for if I tried in on the other it would cause the program to run extremely slowly.

And the way I know it is using a lot of memory is that Java generates an error indicating that it has run out of heap space. I use the -Xmx(memory size)m option. I start at -Xmx1024m then work my way down. I wasn't able to get below 750 without the intern but with intern I could get to 512.

Thanks again.

johndsmitha at 2007-7-14 17:59:09 > top of Java-index,Core,Core APIs...
# 4

> Thanks that worked surprisingly well. It was

> interesting in how I could only apply the intern()

> method on one of my mapping keys for if I tried in on

> the other it would cause the program to run extremely

> slowly.

That's rather surprising.

> And the way I know it is using a lot of memory is

> that Java generates an error indicating that it has

> run out of heap space. I use the -Xmx(memory size)m

> option. I start at -Xmx1024m then work my way down.

> I wasn't able to get below 750 without the intern

> but with intern I could get to 512.

Okay, cool. I'm glad you actually measured first, rather than just assuming. Many people don't.

jverda at 2007-7-14 17:59:09 > top of Java-index,Core,Core APIs...
# 5

Two things

a. I always thought Java Strings use Flyweight pattern and that they are replicated only when required, hence the problem sounds like there is more to it.

b. When you say Maps stores references only, do you mean references to the original object or to a copy of the original object, I have this confusion, becuase as far as I understand the maps retain placeholders, a change in the original Object does not affect the copy referenced by the MAP, does it ?

Anymore light on this is appreciated.

AnmolBa at 2007-7-14 17:59:09 > top of Java-index,Core,Core APIs...
# 6

> Two things

>

> a. I always thought Java Strings use Flyweight

> pattern and that they are replicated only when

> required, hence the problem sounds like there is more

> to it.

Not exactly. String literals go into a pool, so if I have "abc" in my code in one place, and then "abc" in another place, there will only be one String object. However, if I read in a String from a file or user input, or anything other than a literal in the source code, it does not go into the constant pool unless I explicitly call intern() on it.

> b. When you say Maps stores references only, do you

> mean references to the original object or to a copy

> of the original object,

Original object.

jverda at 2007-7-14 17:59:09 > top of Java-index,Core,Core APIs...