JDK-6968657 : IntegerCache should have a minCache value as well as current -XX:AutoBoxCacheMax
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Won't Fix
  • OS: windows_7
  • CPU: x86
  • Submitted: 2010-07-13
  • Updated: 2018-03-21
  • Resolved: 2018-03-21
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
At present the Integer class has an IntegerCache as required by section 5.1.7 in the JLS spec to support caching of Integer instances from -128 to 127. As of version java1.6.0_14 a change was made adding a -XX property:

-XX:AutoBoxCacheMax=<size>

which relates to the system property:

java.lang.Integer.IntegerCache.high

and allows a user to specify a max cache size. The code internally checks this size and so long as it is over the 127 default required by the spec and under an array overflow size it increase the cache range to:

-128 to maxCacheSize.

However, there is no way to increase the range below the -128 value. I wouldve thought that either:

- using the same minimum as provided for the max, ie: -max to max range
- or providing a new property for min cache size

would be good enhancements and very simple to do as this was my first thought that I should be able to specify the minimum value as well as the maximum.

JUSTIFICATION :
The reason for this, is that since a -XX:AutoBoxCacheMax=<size> is provided for max cache size there should be a way of also setting the min cache size.

At present if heavy use is made of the cache although we can increase the range we are stuck at -128 for the minimum. ie: as a max of 10,000 example we have a range of: -128 to 10,000 whereas it seems quite obvious that if we need to increase teh max we also need to increase the minimum.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The addition of a new command line property:

    -XX:AutoBoxCacheMin=<size>

which relates to a new system proprerty :

    java.lang.Integer.IntegerCache.low

and hence allows a larger minimum value for the cache range.

The same checks as currently used for Max cache size would be used for min cache size as well, ensuring that the minimum value was at least -128 as required by the spec at 5.1.7 JLS and that an overflow didnt occur. this code, as mentioned is already in the Integer class for max, in terms of getting the property, determining that its larger than the spec defined minimum, checking it doesnt overflow and then using it are all already for max and would be simple to apply for min.

Also, the Integer.valueOf(int i) method would need a slight chhange to determine when to use the cache and when to use a new Integer(i) constructor. The same thing again is already being done for max where the if clause check is as follows:

if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];

and this check would need to be modified for the Integercache.low as it already checks for IntegerCache.high. Also, the [i + 128] would need to be changed to [i + (-IntegerCache.low)] to deal with the modified minimu value.
ACTUAL -
The actual behavior at present is that there is no ability to set a min cache value and the default of -128 always being used regardless of the Max cache size.

---------- BEGIN SOURCE ----------
Since this is an RFE there is no source code for a test case, but if approved I can provide further code to what Ive already provided for implementing this functionality because as mentioned it is relatively simple to implement as a MAX cache size is already implemented and a Min cache size is simply an extension of that and a duplication of the current max cache code.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
No workaround possible for increasing the size of the cache apart from Implementing your own cache and reffering to this first before going to the java.lang.Integer class.  This is obviously not good as it requires calls to other classes rather than just the default use of the Integer.valueOf(int i) call.

Comments
There is little need for this and the command line and property may be removed in the future.
21-03-2018

It is conceivable that it would be helpful to cache a greater range of negative number as well, but as of yet there has been no pressing need to do so.
20-03-2013