如何增加 Integer 对象的缓存大小

How to increase the cache size for Integer object

阅读本文时question

通过这个评论我知道我们可以增加缓存的大小

The recent version of Java 6 support changing the maximum cache value to greater than 127

我google它但没发现这样做。

请帮忙。

IntegerCache 例如是这样说的:

/**
 * Cache to support the object identity semantics of autoboxing for values between 
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage. During VM initialization the
 * getAndRemoveCacheProperties method may be used to get and remove any system
 * properites that configure the cache size. At this time, the size of the
 * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>.

因此,要么使用 VM 参数,要么使用系统 属性,例如:

-Djava.lang.Integer.IntegerCache.high=256

启动 JVM 时。