java中的对象是否有任何内存大小限制? [英] Does an object in java have any memory size constraints?

查看:371
本文介绍了java中的对象是否有任何内存大小限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以有任何大小的Java对象,没有任何恐惧的异常?我将使用一个类的对象,该对象包含数千个其他对象的 ArrayList ,这些对象包含 HashMap s和 ArrayList s和许多其他非原始类型。



谢谢


Java堆限制了系统中可以拥有的对象的大小。如果对象的大小超过堆大小,则内存不足



在你的case你的总对象的大小(对象在ArrayList +系统中的其他对象)更重要,因为你的ArrayList只是引用这些Object 。



这里是VM选项,您可以使用它来根据您的要求设置堆大小(从 java 文档):


-Xms n



指定
内存分配池的初始大小(以字节为单位)。这个值
必须是1024的倍数,
比1MB大。将字母k或K附加到
表示千字节,或将m或M附加到
表示兆字节。默认值
是2MB。示例:

 
-Xms6291456
-Xms6144k
-Xms6m

-Xmx n



最大大小(以字节为单位)
内存分配池。此值
必须是大于
2MB的1024的倍数。将字母k或K附加到
表示千字节,或将m或M附加到
表示兆字节。默认值
为64MB。示例:

 
-Xmx83886080
-Xmx81920k
-Xmx80m


检查 VM Spec


3.5.3堆 / p>

Java虚拟机具有在所有Java虚拟机>线程之间共享的堆。堆是运行时数据区,从中分配所有类实例和>数组的内存。
堆是在虚拟机启动时创建的。对象的堆存储由>自动存储管理系统(称为垃圾收集器)回收;对象从不>显式释放。 Java虚拟机假定没有特定类型的自动>存储管理系统,并且可以根据实现者的系统需求来选择存储管理技术。堆可以具有固定大小或者可以根据计算所需扩展,并且如果更大的堆变得不必要,则可以收缩。堆的内存不需要是连续的。



Java虚拟机实现可以提供程序员或用户对堆的初始大小的控制,如如果堆可以动态扩展或者收缩,控制最大和最小堆大小。



以下异常条件与堆相关联: / p>

如果计算需要比自动存储管理系统提供的更多的堆,Java虚拟机将抛出一个OutOfMemoryError。


< blockquote>

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

解决方案

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.

这篇关于java中的对象是否有任何内存大小限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆