The Cache
An easy way to see the cache in action is to investigate identity for two small integers compared to numbers outside the scope of the cache. The following two statements both hold true.
Integer.valueOf(17) == Integer.valueOf(17)
and
Integer.valueOf(200) != Integer.valueOf(200)
The java.lang.Integer.IntegerCache and Autoboxing
The cache ensures that there is exactly one Integer instance for each of the 256 integer numbers closest to 0. While this may have impact for GC, a more fundamental effect is how this affects autoboxing as can be seen in the following example.
public class IntegerCacheTest {
private static void test(Integer i, Integer i2) {
System.out.println(i);
if (i == i2) System.out.println(" the same");
if (i != i2) System.out.println(" different");
if (i.equals(i2)) System.out.println(" equal");
}
public static void main(String[] args) {
test(17, 17);
test(200, 200);
}
}
Knowing about the caching of instances for integer 17 but not for 200 the reader may already have expected to get differing results for the two comparisons and indeed, the output is as follows.
17
the same
equal
200
different
equal
What happens is that 17 and 200 are autoboxed when sent as parameters to a method accepting Integers, but for the lower number the cache will ensure we get the identically same instance while the higher number is outside the size limit of the cache and therefore we get two different but equal instances.
Tweaking the Cache Size
The size of the cache can be tweaked with the -XX:AutoBoxCacheMax= option. Thus, if we run the above program setting the cache size to 1000 by adding -XX:AutoBoxCacheMax=1000 to the VM parameters, we get the following result.
17
the same
equal
200
the same
equal
The Cache Internals
The cache is used as follows, as seen in the source code of java.lang.Integer.
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
The actual cache can be found in java.lang.Integer and looks as follows.
...
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
...
Note how the cache is pre-filled on startup rather than on demand, meaning that the memory footprint of the cache is constant no matter which Integers are actually used in the VM. Tweaking the cache to a very large size thus comes with a steep prize in terms of memory consumption.
Interestingly enough, the parameter to set the size of the cache only affects the upper limit and never the lower, meaning that there is no simple way to get Integers smaller than -128 to be interned in the cache.
Interestingly enough, the parameter to set the size of the cache only affects the upper limit and never the lower, meaning that there is no simple way to get Integers smaller than -128 to be interned in the cache.
Edit: There is a reddit thread about this blog post with valuable feedback. For instance, it is pointed out that new instances of small Integers can still be created with the new
operator, a feature that will be deprecated in Java 9.
Also published at DZone: The Internal Cache of Integers.
Hi Dan,
ReplyDeleteI am having a blog on java. Would like to know if you are interested in sharing your knowledge about java and hibernate with my blog readers.
Please let me know your views on this.
Regards
Great Article android based projects
DeleteJava Training in Chennai
Project Center in Chennai
Java Training in Chennai
projects for cse
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
The designer was incredibly attentive; he actually listened to our concerns and addressed them correctly casino utan svensk licens
DeleteWonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.Thanks for sharing this useful post. keep updating..!!!
ReplyDeleteWhite Label Website Builder
ReplyDeletethanks for this excellent article! Just the scripts I've been looking for. a quick question:
how did you create those beautiful charts/graph in your post? many thanks!!!
Java training in Chennai | Java training institute in Chennai | Java course in Chennai
HP2-H86 Exam Dumps Simply make sure your grip on the IT braindumps devised the industry’s best IT professionals and get a 100% guaranteed success in Exam.
ReplyDeleteI am really enjoying reading your well written articles.
ReplyDeleteIt looks like you spend a lot of effort and time on your blog.
Top 10 java training institute in coimbatore |Java Training institute Coimbatore | software Testing Course in Coimbatore | Selenium Training Coimbatore | Automation Testing | Selenium Training institute Coimbatore | Best Selenium Training institute Coimbatore | Online selenium Training institute Coimbatore | software Testing Course in Coimbatore | Best Online software testing training institutes in coimbatore | Best software Testing Training Course in Coimbatore | Online software testing course in coimbatore | Manual and Automation Testing Training in saravanampatti
Here are some tips for your computer that you can watch on DVD.
ReplyDeleteWould you like to make your own film?
Or do you want to burn this series to DVD sooner or later on your computer?
Whatever you want to see, Wondershare DVD Writer will do it all.
In fact, to win (according to Wondershare DVD Carrier Windows 10), drag your favorite movie onto your DVD burner, set the entertainment,
and click the back button.
free Download Link is here:
https://softserialskey.com/wondershare-dvd-creator-crack/
web development training in chennai
ReplyDeleteionic framework training in chennai
node js training in chennai
angular js training in chennai
Lets have a look at a recent news item that is quite
ReplyDeleteentertainmentbee.com
themoviesbio.com
petrefine.com
ReplyDeleteWe will be linking to this particularly great post on our website.
wincracker.com
Aiseesoft FoneLab Crack
WinToUSB Enterprise Crack
Gather Proxy Premium Crack
Kon-Boot Crack
Ami Broker Crack
Bitwig Studio Crack
I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
ReplyDeleteGraph Pad Prism Crack
ReplyDeleteI am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
Wondershare DVD Creator Crack
I thought this was a pretty interesting read when it comes to this topic. Thank you
ReplyDeletealien skin blow up crack
geekbench pro Crack
nextup textaloud Crack
aiseesoft fonelab crack
bluebeam revu extreme Crack
Thecrackfiles.com
This site have particular software articles which emits an impression of being a significant and significant for you individual, able software installation.
ReplyDeletewintousb-enterprise-crack
daemon-tools-pro-crack
sam-broadcaster-pro-crack
utorrent-pro-crack
system-mechanic-pro-crack
Great set of tips from the master himself. Excellent ideas. Thanks for Awesome tips Keep it up
ReplyDeleteGraphPad Prism Crack
UnHackMe Crack
All kinds of apps are available here. Visit my site.
ReplyDeleteI hope it will prove helpful for you.
Good Work! Keep It Up.
It's An Amazing Site.
NextUp TextAloud Crack
IObit Uninstaller Pro Crack
AxCrypt Business Premium Crack
ProPresenter Crack
AnyMP4 Blu-ray Ripper Crack
ReviverSoft Disk Reviver Crack
Driver Talent Pro Crack
PDF XChange Editor Crack
AirServer Crack
My response on my own website. Appreciation is a wonderful thing...thanks for sharing keep it up. Adobe Audition CC Crack
ReplyDeleteNexus Expansion Pack Crack
Marmoset Hexels Crack
UnHackMe Crack
<a
Thanks For Sharing...Such a nice post...
ReplyDeleteWinToUSB Enterprise Crack
FxSound Enhancer Premium Crack
PUSH Video Wallpaper Crack
I am very happy to read this article. Thanks for giving us Amazing info. Fantastic post.
ReplyDeleteThanks For Sharing such an informative article, Im taking your feed also, Thanks.wondershare-dvd-creator-crack/
My response on my own website. Appreciation is a wonderful thing...thanks for sharing keep it up.
ReplyDeleteBitwig Studio Crack
Bitdefender Total Security Crack
Plagiarism Checker X Crack
WinISO Crack
NetBalancer crack
I was looking for this information from enough time and now I reached your website it’s really good content.
ReplyDeleteThanks for writing such a nice content for us.
2021/07/12/sam-broadcaster-pro-crack
Thanks For Sharing...Such a nice post...
ReplyDeleteAiseesoft FoneLab Crack/
RadioBOSS Crack
Enigma Recovery Professional Crack
Epic Pen Pro Crack
Glary Utilities Pro
I am very happy to read this article. Thanks for giving us Amazing info. Fantastic post.
ReplyDeleteThanks For Sharing such an informative article, Im taking your feed also, Thanks.
deepl-pro-crack
ReplyDeleteI am very happy to read this article. Thanks for giving us Amazing info. Fantastic post.
Thanks For Sharing such an informative article, Im taking your feed also, Thanks.viddly-youtube-downloader-plus-crack/
Wow, amazing block structure! How long
ReplyDeleteHave you written a blog before? Working on a blog seems easy.
The overview of your website is pretty good, not to mention what it does.
In the content!Thank you for your valuable article. Please keep it up since information is endless and I eagerly await another amazing essay like that.I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
Very interesting blog.Thank you for sharing this information..
Bitwig Studio Crack
AnyDesk Premium Crack
Autodesk AutoCAD LT Crack
Hot Alarm Clock Crack
Enscape 3D Crack