Which is worse?
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!
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
String thing = "asdfasdfadf"
thing.replace(thing, "new text");
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Could be, but it's more likely with the way you treat them, how you process them.
Or it is with the network...
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Paul thanks for the help. I can tell you spent a lot of time, and I appreciate that. The post I made about the webcrawler is irrelevant. I've already got it up and running, but I'm having this speed problem. I'm going to try the garbage collection statement you recommended. I'll post back with details.
Anyway, you were saying something about the way I'm calling the threads. I didn't think of that, but it's a great possibility. See, I was calling it each time for each URL, so I was actually calling that thread 2000 times to get 2000 results.
Here's what I was thinking:
I could have one thread that parses through and gets all of the URls from the starting URL. This thread would only be called once.
After that, I can create a thread for each of those URLs, and that be that. I think I would be using no more than 20-30 threads.
Note: I can post the working version of the webcrawler if you like, but I working on a newer one.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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
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
java -verbose:gc ClassName
A nice list of free java profilers
Ok, I tried the verbose and I got 5 GC's in 30 searches. I don't think that's too good. At the points where it happens I do notice a slight pause. Some some performance is lost there.
How do I get the results from the java -prof command? It said it was dumpinig the data, but I don't know where to.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20