Maximum Classpath Length

From EggeWiki
Revision as of 18:04, 31 March 2009 by Brianegge (talk | contribs) (New page: In looking at [Ssh argument length], I decided to figure out how many classes I could put on the Java classpath using Solaris. So, I coded this little bash script which creates tiny one...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In looking at [Ssh argument length], I decided to figure out how many classes I could put on the Java classpath using Solaris.

So, I coded this little bash script which creates tiny one class jars, and then attempts to load them.

<geshi lang="bash">

  1. !/bin/bash

TMP="${HOME}/tmp/cp/" mkdir -p $TMP cd $TMP

echo "Checking for maximum classpath length"

cat <<EOF > CheckClasspath.java public class CheckClasspath {

 public static void main(String args[]) {
   try {
     Class.forName(args[0]);
     System.out.println("Found class " + args[0]);
   } catch (ClassNotFoundException e) {
     System.err.println("Failed to find class " + args[0]);
     System.exit(1);
   }
 }

} EOF

javac CheckClasspath.java java -cp . CheckClasspath CheckClasspath || exit 1

CP="." for i in $(seq 1 100000); do

 if [ ! -f "C$i.jar" ]; then
   cat <<EOF > C$i.java

public class C$i { } EOF

   javac C$i.java
   jar cf C$i.jar C$i.class
   /bin/rm C$i.java C$i.class
 fi
 CP="$CP:C$i.jar"
 if [ $(($i%100)) -eq 0 ]; then
   java -d64 -Xms8g -Xmx8g -cp "$CP" CheckClasspath C$i || exit 1
 fi

done </geshi>

Using a default 1GB 32 bit JVM, I was able to put 8600 jars. Increasing the memory to 2GB allowed me to about double this and get to 16700. Then I switched to an 8GB 64-bit JVM. At 37818 jars my test case would take about five minutes to execute. I didn't test how many classes I could load into the JVM, but I suspect quite a few more than I could jars. At any rate, one doesn't need to worry about Java truncating the classpath, but rather it's the usual memory and CPU constrains that put an upper limit on the jars.