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!

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.

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?

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.

Strings are immutable. Meaning you can NEVER change them.
So if I do this:

String s1 = "Pig";
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:

String s1 = "Pig";
String s2 = "Horse";
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.

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.

Could be, but it's more likely with the way you treat them, how you process them.

Or it is with the network...

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

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

I don't know if theres a way in java to get a count of the current treads, but you could implement a singleton patterns (only not restrict to a single instance and not returning a class but running a tread, I guess its not really a singleton pattern but its along the same lines) if theres too many treads wait

Singleton Pattern
Singleton Implementation

If you get it working faster please tell us what was making it slow, Be interesting to find out.. Good Luck

Dang 30 minute edit limit :P

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.

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.

I think it should be in java.prof.

-prof Starts Java runtime with java(1) profiling
enabled. By default, this puts profile
results in the file ./java.prof. This option
only works with java_g.

-proffile Starts Java runtime with java(1) profiling
enabled. This form of the option allows the
user to specify a different output file for
the profile information. For example, the
option -prof:myprog.prof enables profiling
and puts the profile results in the file
myprog.prof rather than in the default file
./java.prof.

Outputs a space dilimited file with the field names at the top.

Might be worth opening it with Excel or Openoffice calc using space delmimnation... Might be abit neater

This article has been dead for over six months. Start a new discussion instead.