[jdom-interest] JDOM and memory

Rolf Lear jdom at tuis.net
Sat Jan 28 14:56:47 PST 2012


(I did a reply, not reply all, so it did not go to the list).

I disagree.... it is element names, attribute names, and in the case of 
SlimJDOMFactory, it is the XML Text content (whitespace padding between 
elements is ripe for reuse). If you put JDOM in something like a TomCat 
server with long-running applications, the PermGen space dies pretty 
fast.... especially with the way that tomcat has multiple classloaders, etc.

I consider it to be bad practice for a library to make routine use of 
the PermGen space.

I modified the example slightly... added timing to it... and then I 
compared it to the StringBin tool I built.... ;-)

Here are the two code examples:

public class OOM {
	public static void main(String[] args) {
		int i = 0;
		String[] strings = new String[10000000];
		long time = System.currentTimeMillis();
		try {
			while (true) {
				i++;
				strings[i] = ("Number " + i).intern();
				if (0 == (i % 100000)) {
					System.out.printf("%s at %.4f/ms\n", strings[i], (1.0 * i) / 
(System.currentTimeMillis() - time));
				}
			}
		} catch (Error t) {
			System.out.println("Last was " + i);
			throw t;
		}
	}
}



and second example:

public class OOMSB {
	public static void main(String[] args) {
		int i = 0;
		String[] strings = new String[10000000];
		StringBin sb = new StringBin();
		long time = System.currentTimeMillis();
		try {
			while (true) {
				i++;
				strings[i] = sb.reuse("Number " + i);
				if (0 == (i % 100000)) {
					System.out.printf("%s at %.4f/ms\n", strings[i], (1.0 * i) / 
(System.currentTimeMillis() - time));
				}
			}
		} catch (Error t) {
			System.out.println("Last was " + i);
			throw t;
		}
	}
}


The String.intern() fails at:
Number 500000 at 99.1080/ms
Number 600000 at 79.0306/ms
Number 700000 at 65.1405/ms
Number 800000 at 54.7608/ms
Number 900000 at 46.9851/ms
Number 1000000 at 40.9920/ms
Last was 1043637
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
	at java.lang.String.intern(Native Method)
	at net.tuis.debug.OOM.main(OOM.java:12)



The StringBin version fails at.....

Number 9500000 at 693.2788/ms
Number 9600000 at 697.7758/ms
Number 9700000 at 701.9829/ms
Number 9800000 at 706.4081/ms
Number 9900000 at 596.7810/ms
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
10000000
	at net.tuis.debug.OOMSB.main(OOMSB.java:15)



Another reason to not use String.intern..... it is slow... ;-)

Rolf


On 28/01/2012 5:08 PM, Paul Libbrecht wrote:
>
>
>
> Rolf Lear<jdom at tuis.net>  a écrit :
>
>> ... Java 6 does GC in the perm-gen space.... I watched it clearing out
>> the values in the JVisualVM monitor.... but keeping a reference to the
>> intern'd string causes OOM as expected.
>
> very cute example, thanks for that!
>
>> In many places 1,000,000 strings is not a lot....
>
> I agree, but not in element names!
>
> paul
>



More information about the jdom-interest mailing list