|
|
Re: How to make program eat less memory/enhance performance ?
|
Posted: Oct 20, 2003 7:33 PM
|
|
[I thought I replied to this earlier, but the reply got eaten somehow.]
> Doesn't Java use a UTF-8 representation internally?
java.lang.String stores an array of 2-byte characters. JNI can convert strings to UTF-8, and of course I/O can write UTF-8, but the in-memory representation is UTF-16.
Similarly, I'm not sure porting to C will help. Yes, each string takes up half the bytes, but as someone above pointed out, the real problem is how memory consumption grows as the data set gets bigger.
Also, the suggested rewrite above, using StringBuffer, won't improve matters. The compiler already converts string concatenation into an expression using a StringBuffer.
My suggestions:
1. Defer converting elements in the matrix to Strings until the last possible moment. If possible, write the string representation directly to a Writer, and don't keep it afterwards.
2. If you're using the String as a hash key, create a class that contains references to the elements from which you compose the string, implement that wrapper's hashCode() and equals() methods, and use instances of that as a hash key. A quick if sub-optimal implementation of hashCode() would xor ("^") together the hash codes of each component referred to.
3. Read Jack Shirazi's book Java Performance Tuning, or check the website http://www.javaperformancetuning.com/. It's mostly about time optimization, but it also discusses space.
|
|