Does an object in java have memory limits?

Can we have any size of a Java object without fear of exception? I am going to use an object of a class that consists of an ArrayList thousands of other objects containing pairs of HashMap and ArrayList and many other non-primitive types.

thanks

+7
java collections
source share
7 answers

Java Heap is a limit on the size of objects that you can have on the system. If the size of your object is outside the heap, then an "Out of memory" error is generated.

In your case, your total size of the object (the object in ArrayList + other objects in your system) matters more, since your ArrayList will simply refer to these objects.

Here are the VM parameters that you can use to set the heap size according to your requirement (from the java documentation ):

-Xms p

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

        -Xms6291456
        -Xms6144k
        -Xms6m

-Xmx p

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

        -Xmx83886080
        -Xmx81920k
        -Xmx80m

Check heap contents from VM specification

3.5.3 Heap

The Java virtual machine has a bunch that is common to all Java virtual machines. The heap is the runtime data area from which memory is allocated for all instances of the class and> arrays. The heap is created when the virtual machine starts. Heap storage for objects is regenerated by the automatic storage management system (known as the garbage collector); objects are never> explicitly released. The Java virtual machine does not accept a specific type of automated storage management system, and storage management technology can be selected in accordance with the requirements of the developing system. The heap can have a fixed size or it can be> expanded as required by the calculation, and can be reduced if a large heap becomes unnecessary. Heap memory does not have to be contiguous.

Implementation of the Java virtual machine can give the programmer or user control over the initial heap size, and also, if the heap can dynamically expand or> shrink, control the maximum and minimum heap size.

The following exceptional condition is associated with the heap:

If more heap is required for the calculation than can be provided by the automatic storage management system, the Java virtual machine throws an OutOfMemoryError.

+2
source share

If you have an object (let it call it A ) that references an ArrayList with many, many objects in it, the size of " A will still be pretty small (link size plus a bit of overhead). The objects referenced by A , almost independent of A The only limitation is that the total size of all objects is limited by available memory.

The only truly "huge object" will be one with many, many fields, but there the JLS / JVM specification sets a rather small limit ( fields_count in the class file format is u2 , so you can have no more than 65,535 fields).

+13
source share

You cannot use an object of size without consequences. You can code what you like, but obviously you need to know about the JVM and the typical memory / heap restrictions used in it.

0
source share

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

0
source share

Of course, he has limited memory limits. However, you can manage heap memory by initializing a higher size. But this does not guarantee that you can use unlimited memory as you like.

0
source share

The total heap limit is the main memory limit. As long as your objects fit into this, you will be fine.

You can check this if you want by allocating a large number of large arrays and observing when you get OutOfMemoryErrors.

There is also a limit to the size of the array 2147483647 due to the size of integer indices. However, in fact, no one saw anyone come across this in practice.

0
source share

The memory size of an object is based on the memory size of the heap. An object can hold up to heap memory size. Follow the link. http://javarevisited.blogspot.in/2011/05/java-heap-space-memory-size-jvm.html p>

0
source share

All Articles