• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help needed on String.intern()  RSS feed

 
Murali Su
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read in some java forum posts that String.intern() may throw OutOfMemoryError for very large number of Strings.

My application has to deal with a large number of String objects (~80K-90K)-like reading a large spreadsheet. The input -say a large excel file- could contain lot of duplicate Strings which is good for interning. But it could also contain distinct Strings in which case, an OutOfMemoryError could occur if we intern it -because JVM keeps on adding them to the String pool. (as per those posts)

One way to overcome this is by increasing the memory size using the -X options, but I want to know if this problem still exists. (All the posts were about 5-6 years old -1999/2000). Have the later JVMs(1.4.x) overcome this problem may be using techniques similar to weak references, better GC for String pools etc? Also, are there any dis-advantages (speed??) of using String.intern()?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modern JVMs do indeed seem to use weak references (or something equivalent) in their intern pools. Disadvantages - well, it takes nonzero time to call the intern() method, but it's rarely large enough to be an issue. And it's possible some JDKs might not allow interned strings to be collected at all. But when we discuss GC, all sorts of things are possible in different implementations. I recommend you try interning strings as you propose, and test the results under high volume. You may even find it useful to limit the memory, for testing, in order to more easily force a memory shortage to see what happens.
 
Murali Su
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Jim, I 'll experiment with various memory sizes and see.
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, basically your application doesn't have to deal with Strings at all. That a choise of yours.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
Well, basically your application doesn't have to deal with Strings at all. That a choise of yours.


Well, if we want to nitpick: you can't keep a java application from at least creating the String[] that gets passed to the main method.

Anyway, I'm not sure how this is going to help the original poster...
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the only reason I can find is that you're trying to promote your book isn't it?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"U", please check your private messages. Don't want to hijack a thread, again...
 
u johansson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Illya Preuss:

My advice to the OP was to reconsider the extensive use of Strings. Without knowing the exact details it's often a performance bottleneck.

Regarding your nitpicking - a String array isn't a String. If you feel you need to nitpick make sure you have a case!

Finally I think you should stop sneaking in advertisments for your book. It's extremely annoying and it beats me how people can be so full of themeselves that they don't understand that. If you want to promote something for your own financial gain you should pay pay for ads like everybody else.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by u johansson:
My advice to the OP was to reconsider the extensive use of Strings.


That wasn't obvious to me from your original post, sorry.

What alternatives do you see?

Regarding your nitpicking - a String array isn't a String. If you feel you need to nitpick make sure you have a case!


Point taken.

Regarding my signature, this isn't the right place to discuss it. Refer to my private message, or start a new thread in the JavaRanch forum if you prefer to discuss this in public, please.
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[deleted]
[ January 01, 2006: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulrika, or Ulf, or whichever you prefer - please read the private messages that were sent to your original account. (longin uj, not login ulfjo721). And learn some manners, or your accounts will be closed. Thank you.
[ January 01, 2006: Message edited by: Jim Yingst ]
 
paul wheaton
Trailboss
Posts: 22553
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear u/uj/whatever ...

I am the owner of this site. If you don't believe me, scroll to the bottom of any page and look at the copyright message.

The two people you are irritating are members of the staff here. Note the title of "sheriff". Good, decent people who are trying to keep things pleasant here at JavaRanch. Another title to watch for is "bartenter". Bartenders moderate one forum. Sheriffs moderate all forums. Sheriffs also have the ability to close accounts - as you have learned.

When you signed up, you agreed to "be nice". In order to understand what "be nice" means, we need to communicate. Preferably privately. Since you don't provide an e-mail, PM is used. Since you refuse to read the PMs ... well, your first account was closed to get your attention. After your last post, I would be surprised if your second account isn't closed. And now communication with you is happening in a public place because that appears to be all that is left.

There are lots of places on the internet where people get mighty angry. This isn't one of them. These forums are heavily moderated. I like it that way. And it seems lots of folks like it that way.

I suppose you can keep creating new accounts and we'll keep deleting your posts and closing your accounts. Or, maybe you'll find a different place to hang out. Or .... maybe you'll create a new account, blend in a bit, and try to understand why we do it this way.

I dug up this old bit of text. Maybe it'll help, maybe it won't ...


Because of language and cultural differences, the true meaning of a message over this medium can be easily mis-interpretted.

I think a great way to tell a person's character is to see if they are finding the best possible interpretation of a message, or the worst. Icky people tend to find the worst possible interpretation. Decent folk tend to find the best possible interpretation.

Another great way to tell a person's character is to see if they try to make ugly situations better or worse.

And yet another great way to find the quality of a person's character is how they behave when confronted with an actual personal attack: do they blow their top, or do they brush it aside. A person that brushes it aside clearly does not value the opinion of the person that is attacking them and feels good enough about themselves that such an attack seems petty and not worth a response.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!