Community
Participate
Working Groups
The constructor for org.eclipse.jdt.internal.core.ClassFile has an interesting space optimization: // don't hold on the .class file extension to save memory // also make sure to copy the string (so that it doesn't hold on the // underlying char[] that might be much bigger than necessary) this.name = new String(name.substring(0, name.length() - 6)); An interesting idea, but on the VM I checked, this does not save any memory at all. The original character array is held onto along the calls to substring and the copy constructor. As a result, we are doing extra work and do not save any memory.
I remember jerome fixing a couple of these already.
I wanted to avoid: StringBuffer buffer = new StringBuffer(ame.substring(0, name.length() - 6)); this.name = buffer.toString(); which is even more costly. Would it be possible for the VM you're using to change its behavior and do the copy of the char array if its length is greater than the String's size ?
The StringBuffer example actually wastes even more space. The only guaranteed way to reduce space is to use getChars(). This will of course cause 2 copies but you will always have the smallest amount of space used.
Note this code has moved to PackageFragment#getClassFile(String). Changed PackageFragment#getClassFile(String) to use getChars(...).
Code verified for 3.2 M5 using build I20060214-0010.