Which is worse?

Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Which is worse?

 
0
  #1
May 12th, 2005
Ok, I'm working with some Strings, and these Strings will get remade up to an infinite number of times, and I know that a new object is created each time.
So, which is worse..

Keep using the String, and have the value continously change, or

Use a StringBuffer, and continously delete the current text so that the upcoming read text can be the only thing in the StringBuffer.

I hope that makes sense!
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Which is worse?

 
0
  #2
May 12th, 2005
String thing = "asdfasdfadf"

thing.replace(thing, "new text");
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Which is worse?

 
0
  #3
May 13th, 2005
The Strings will all take up memory as they'll be stored in the String constant pool.
The next time an identical String is used it will be pulled from the pool instead.

This assumes you do NOT use the constructors for String explicitly as that prevents the pool from being used.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Which is worse?

 
0
  #4
May 13th, 2005
What about using Strings like I've been doing. Assigning it a new value each time, and I guess it creates a new Object. Will Java's automatic garbage collection not take care of the unused objects?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Which is worse?

 
0
  #5
May 13th, 2005
If each String is different from all the others then yes, a new one is created each time.
When and if they're deleted at some point is up to the JVM implementation.

I'm not quite sure what you're planning to do with a StringBuffer instead.
If you just add String literals to it it won't save any memory, if the Strings are retrieved on the fly from external sources (say incoming network data) then you should indeed most likely prefer StringBuffers.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jul 2003
Posts: 117
Reputation: Iron_Cross is an unknown quantity at this point 
Solved Threads: 2
Iron_Cross's Avatar
Iron_Cross Iron_Cross is offline Offline
Junior Poster

Re: Which is worse?

 
0
  #6
May 13th, 2005
Strings are immutable. Meaning you can NEVER change them.
So if I do this:
  1. String s1 = "Pig";
  2. s1 += "Horse";
I've just made 3 permenant strings. I've made "Pig" "Horse" and "PigHorse". All strings are constants, so they will never be changed. When you change a reference to string by reassigning it a new string, you've created a new one. For example:
  1. String s1 = "Pig";
  2. String s2 = "Horse";
  3. s1 = "Cow";
You now have 3 strings, you have "Pig" "Horse" and "Cow".

If you're going to be working with strings, use a stringbuffer. You will gain far more performance by doing that then using string objects.
elitehackers.info
Today's Penny-Arcade!
Pain is weakness leaving the body!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Which is worse?

 
0
  #7
May 13th, 2005
Actually, this was about network data. I've made a webcrawler and finally got it to work, but it's extremely slow.

See, I read the HTML code one line at a time, and this one String is set equal to the read line. So, If I have hundreds of lines of code, and thousands of URLs, then I know part of my speed problem is with the Strings.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Which is worse?

 
0
  #8
May 14th, 2005
Could be, but it's more likely with the way you treat them, how you process them.

Or it is with the network...
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Which is worse?

 
0
  #9
May 14th, 2005
I have played around with, A little while ago, an app (a profiler) that checks how optermised your java code is (tells you a range of information on each object, Like memory, and cpu time etc..) something like this would be great for figuring out exactly what is taking alot of time to process..

I might even be worth making some code to calculate the time difference so you can find the time it takes to read the string and the time it takes to process that string etc..

Perhaps try the java interpreters profiler

  1. java -prof ClassName

The best approch for strings is prolly using the same one over and over again, the garbage collection process and the object creation both take time

Too see howmuch your using garbage collection use

  1. java -verbose:gc ClassName


A nice list of free java profilers
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Which is worse?

 
0
  #10
May 14th, 2005
Just looked at the last post if its still the same
you can avoid making a string
URL newURL = new URL(text.substring(numFirst+9, numEnd));


also are you still running run() from the tread, Im just thinking running to many treads could be similar to running too many forks (forkbombing) it may make the JVM just die in the butt attempting to figure out who to give time, I have never seen what happens if you run alot of treads so im just making assumptions.

But i have a suspision that there is a strong possability that thats why its happing
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC