Search code examples
javacachingjava-6

How to increase the cache size for Integer object


While reading this question

I come to know that we can increase the size of cache through this comment

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

I google it but didnt find to do same.

Please help.


Solution

  • IntegerCache for e.g. says this:

    /**
     * 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>.
    

    So either you use VM parameter or you use System property like:

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

    When launching the JVM.