views:

151

answers:

6

Can we have any size of Java object without any fear of exception? I am going to use an object of a class that consists of an ArrayList of thousands of other objects that contains couples of HashMaps and ArrayLists and many other non primitive type.

Thank you

A: 

You can't use any size object without repercussions no. You can code what you like, but obviously you need to be aware of the JVM and the typical memory / heap limits employed therein.

Ben Poole
A: 

The only limiting factor is the maximum heap size, I also had few 100MB fat object as an in memory db.

stacker
A: 

Ofcourse it has memory limit constraints. However you can control the heap memory by initializing to a higher size. But that does not guarantee that you can use unlimited memory as you like.

Bragboy
+10  A: 

If you have an object (let's call it A) that references an ArrayList with many, many objects in it, the "size" of A will still be rather small (the size of the reference plus a bit of overhead). Objects referenced by A are pretty much independent from A. The only limit is that the total size of all objects is limited by the available memory.

The only truly "huge object" would be one with many, many fields, but there the JLS/JVM spec sets a pretty small limit (the fields_count in the class file format is an u2 field, so you can have at most 65 535 fields).

Joachim Sauer
Damn I was just designing an object with 65536 fields :(
fish
@fish: that's not design, that's an accident ;-)
Joachim Sauer
+3  A: 

Java Heap is limit for size of objects those you can have in system. If your object's size is beyond heap then Out Of Memory error would be generated.

In your case your total object's size (Object's in ArrayList + other objects in your system) matters more, As your ArrayList would be just referencing these Object's.

Here are VM options you can use to set Heap Size as per your requirement (from the java documentation):

-Xmsn

Specify the initial size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 1MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 2MB. Examples:

       -Xms6291456
       -Xms6144k
       -Xms6m

-Xmxn

Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. Examples:

       -Xmx83886080
       -Xmx81920k
       -Xmx80m

Check Heap info from VM Spec

3.5.3 Heap

The Java virtual machine has a heap that is shared among all Java virtual machine >threads. The heap is the runtime data area from which memory for all class instances and >arrays is allocated. The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by >an automatic storage management system (known as a garbage collector); objects are never >explicitly deallocated. The Java virtual machine assumes no particular type of automatic >storage management system, and the storage management technique may be chosen according >to the implementor's system requirements. The heap may be of a fixed size or may be >expanded as required by the computation and may be contracted if a larger heap becomes >unnecessary. The memory for the heap does not need to be contiguous.

A Java virtual machine implementation may provide the programmer or the user control over >the initial size of the heap, as well as, if the heap can be dynamically expanded or >contracted, control over the maximum and minimum heap size.5

The following exceptional condition is associated with the heap:

If a computation requires more heap than can be made available by the automatic storage management system, the Java virtual machine throws an OutOfMemoryError.

YoK
A: 

The overall heap limit is the main memory constraint. As long as your objects fit within this you will fine.

You can test this if you like by allocating lots of big arrays and observing when you get OutOfMemoryErrors.

There is also an array size limit of 2147483647 due to the size of integer indexes. Never actually seen anyone run into it in practice however.

mikera