Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Integer class caching values in the range -128 to 127?

Tags:

java

caching

Regarding my previous Question, Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128? , we know that Integer class has a cache which stores values between -128 and 127.

Just wondering, why between -128 and 127?

Integer.valueOf() documentation stated that it "caching frequently requested values" . But does values between -128 and 127 are frequently requested for real? I thought frequently requested values are very subjective.
Is there any possible reason behind this?

From the documentation also stated: "..and may cache other values outside of this range."
How is this can be achieved?

like image 558
DnR Avatar asked Jan 03 '14 05:01

DnR


People also ask

What is caching range in Java?

Java Integer Cache Implementation: In Java 5, a new feature was introduced to save the memory and improve performance for Integer type objects handling. Integer objects are cached internally and reused via the same referenced objects. This is applicable for Integer values in the range between –128 to +127.

What is integer caching in Python?

Python caches small integers, which are integers between -5 and 256. These numbers are used so frequently that it's better for performance to already have these objects available. So these integers will be assigned at startup. Then, each time you refer to one, you'll be referring to an object that already exists.

What is integer pool?

Integer constant pool is used to cache frequently used integer objects to increase performance while processing Integers. Integer. valueOf(int) method provides the Integer from Integer constant pool (if available) rather than creating new object. so Integer.

Which class is used in Java for storing caches?

JCache is a Java API for caching. It provides a set of common interfaces and classes that can be used to temporarily store Java objects in memory. It is a JCP standard represented by JSR 107.

Why between-128 and 127 integers must be cached?

Just wondering, why between -128 and 127? A larger range of integers may be cached, but at least those between -128 and 127 must be cached because it is mandated by the Java Language Specification (emphasis mine):

What is the range for integer caching?

This is applicable for Integer values in the range between –128 to +127. This Integer caching works only on auto-boxing. Integer objects will not be cached when they are built using the constructor.

What is integer caching in Java?

Integer objects are cached internally and reused via the same referenced objects. This is applicable for Integer values in the range between –128 to +127. This Integer caching works only on auto-boxing. Integer objects will not be cached when they are built using the constructor.

What are the default values for integercache in Java?

Default values for IntegerCache.low and IntegerCache.high are -128 and 127 respectively. In other words, instead of creating and returning new integer objects, Integer.valueOf () method returns Integer objects from an internal IntegerCache if the passed int literal is greater than -128 and less than 127. * {@code int} value.


1 Answers

Just wondering, why between -128 and 127?

A larger range of integers may be cached, but at least those between -128 and 127 must be cached because it is mandated by the Java Language Specification (emphasis mine):

If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

The rationale for this requirement is explained in the same paragraph:

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. [...]

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.


How can I cache other values outside of this range.?

You can use the -XX:AutoBoxCacheMax JVM option, which is not really documented in the list of available Hotspot JVM Options. However it is mentioned in the comments inside the Integer class around line 590:

The size of the cache may be controlled by the -XX:AutoBoxCacheMax=<size> option.

Note that this is implementation specific and may or may not be available on other JVMs.

like image 54
assylias Avatar answered Oct 18 '22 17:10

assylias