No subject


Sat Nov 12 19:29:10 PST 2005


>         // dies @ 41943000
>     Integer foo3 = new Integer(32767); // 16 bytes
                                              _bits_, right?
>         // dies @ 41943000
>     Integer foo4 = new Integer(2147483646); // 32 bytes
                                                   _bits_, right?
>         // dies @ 41943000

A java int should consist of 32 bits regardless of its quantity.  All integral literals unless explicitly casted (except integral literals that end in "L", they're longs, I believe) are ints.

> System.out.println("Starting: " + args[0]);
>     try{
>         for (int i=0; i< new Integer(args[0]).intValue(); i++){
>                 int modTest = i % 1000;
>                 if (modTest == 0){
>                         System.out.print(i + ".");
>                 }
>                 test1.add(foo4);
>         }
> System.out.println("\nDone: " + args[0]);
>     }
>     catch (Exception e){
>         e.printStackTrace();
>     }
> 
> Then I ran it and was able to get up to about 41.9 million runs
> before I got a memory exception. What that
> calculated out to was for the strings was
> 41.9 million * 100 bytes / 1K = 4.09 gigabytes

You've really only stored one character array in memory.
 
> and I only have 1 gigabytes of memory in this machine
> with WAY less than 3 gigabytes in swap...
> 
> (I'm using a P3 800Mhz running NT 4 sp6a and
> with 1 GB of memory)
>
> So, what is going on? Is Java not really allocating all
> the memory I think it is? Is it compressing things
> because I am inserting the same string over and over again?
> You can see I've tried Strings and Integers.

Well, each String you add to your Vector adds only an object reference.  From JDK API documentation: "Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared."  You're not storing multiple arrays of characters, you're storing pointers to one array of characters.  Again, with the Integers, you're storing multiple references to the same object.

To make your test more effective, you may want to try something like this:

>         for (int i=0; i< new Integer(args[0]).intValue(); i++){
>                 int modTest = i % 1000;
>                 if (modTest == 0){
>                         System.out.print(i + ".");
>                 }
-                 test1.add(foo4);
+                 test1.add(new Integer(i));
>         }

No guarantees on that one, either, but it might be a more effective test.  (Good idea to use a changing int like your index just in case Integer keeps a list of instances and returns previously created ones when the same value is supplied).

> Is there an upper limit on Vector size, or array, or is it
> simply the ammount of memory in the machine?

As far as number of elements, it probably should be the positive values of int (2^31).  I would imagine that there is no "size" limitation on Vector, but the JVM's allocated memory cannot exceed the capacity of the machine it's on.

-Brian



More information about the Advanced-java mailing list