當前位置:
首頁 > 知識 > Java之戳中痛點-(7)善用Java整型緩存池

Java之戳中痛點-(7)善用Java整型緩存池

先看一段代碼:

package com.test;

import java.util.Scanner;

public class IntegerCache {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(input.hasNextInt){
int ii = input.nextInt;
System.out.println("===" + ii + "的相等判斷 ===");
//通過兩個new產生的Integer對象
Integer i = new Integer(ii);
Integer j = new Integer(ii);
System.out.println("new的對象:" + (i == j));

//基本類型轉換為包裝類型後比較
i = ii;
j = ii;
System.out.println("基本類型轉換的對象:" + (i == j));

//通過靜態方法生成的一個實例
i= Integer.valueOf(ii);
j = Integer.valueOf(ii);
System.out.println("valueOf的對象:" + (i == j));
}
}
}

結果:

Java之戳中痛點-(7)善用Java整型緩存池

看看不同數據的測試結果,如果你感興趣可以測試一下其他的數據,最後發現-128 到 127 基礎類型轉化的對象和valueOf轉化的對象 == 是 true

下面解釋一下原因:

1、new產生的Integer對象

new聲明的就是要生成一個新的對象,2個對象比較內存地址肯定不相等,比較結果為false

2、裝箱生成的對象

對於這一點首先要說明的是裝箱動作是通過Integer.valueOf方法進行的。

Integer i = 100; (注意:不是 int i = 100; )

實際上,執行上面那句代碼的時候,系統為我們執行了:Integer i = Integer.valueOf(100); 此即基本數據類型的自動裝箱功能。

valueOf如何生成對象:

public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }

這是JDK的源碼,low=-128,h=127,這段代碼意為如果是-128到127之間的int類型轉換為Integer對象,則直接從IntegerCache里獲取,來看看IntegerCache這個類

private static class IntegerCache {
static final int low = -128;
static final int high;
// 內部靜態數組,容納-128到127之間的對象
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 {}
}

結論:

在創建Integer對象,盡量少用new創建,盡量使用valueOf,利用整型池的提高系統性能,通過包裝類的valueOf生成包裝實例可以提高空間和時間性能。

另外:在判斷對象是否相等的時候盡量使用equals方法,避免使用「==」產生非預期結果。

附1:

Integer 可以通過參數改變緩存範圍(附:最大值 127 可以通過 JVM 的啟動參數 -XX:AutoBoxCacheMax=size 修改)

-XX:AutoBoxCacheMax 這個參數是設置Integer緩存上限的參數。

理論上講,當系統需要頻繁使用Integer時,或者說堆內存中存在大量的Integer對象時,可以考慮提高Integer緩存上限,避免JVM重複創造對象,提高內存的使用率,減少GC的頻率,從而提高系統的性能

理論歸理論,這個參數能否提高系統系統關鍵還是要看堆中Integer對象到底有多少、以及Integer的創建的方式,如果堆中的Integer對象很少,重新設置這個參數並不會提高系統的性能。

即使堆中存在大量的Integer對象,也要看Integer對象是如何產生的

1. 大部分Integer對象通過Integer.valueOf產生。說明代碼里存在大量的拆箱與裝箱操作。這時候設置這個參數會系統性能有所提高

2. 大部分Integer對象通過反射,new產生。這時候Integer對象的產生大部分不會走valueOf方法,所以設置這個參數也是無濟於事

附2:

其他緩存的對象:

這種緩存行為不僅適用於Integer對象。我們針對所有整數類型的類都有類似的緩存機制。

Byte 有 ByteCache 用於緩存 Byte 對象(-128 到 127)

Short 有 ShortCache 用於緩存 Short 對象(-128 到 127)

Long 有 LongCache 用於緩存 Long 對象(-128 到 127)

Character 有 CharacterCache 用於緩存 Character 對象(0 到 127)

Float 沒有緩存

Doulbe 沒有緩存

除了 Integer 可以通過參數改變範圍外,其它的都不行。

附3:

關於垃圾回收器:

Integer i = 127;

i = null; //不會被垃圾回收器回收,這裡的代碼不會有對象符合垃圾回收器的條件,i 雖然被賦予null,但它之前指向的是cache中的Integer對象,而cache沒有被賦null,所以Integer 127這個對象還是存在的

Integer i = 200;

i = null; //會被垃圾回收器回收,而如果 i 大於127或小於-128,則它所指向的對象將符合垃圾回收的條件

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 科技優家 的精彩文章:

對於JSONObject,我只是臨時抱佛腳
linux exec操作文件描述符
python協程1:yield的使用
Azure經典門戶創建VM,如何設置使用靜態IP地址?
Mina 報文監聽器NioDatagramAcceptor一

TAG:科技優家 |

您可能感興趣

Hitachi Vantara升級Skylaking伺服器加入Optane緩存和GPU
使用RedisTemplate(JDK序列化策略)緩存實體類
MyBatis中的緩存
TinyShop緩存文件獲取WebShell之0day
SpringBoot:SpringDataRedis緩存改造
Flutter圖片緩存 Image.network源碼分析
英特爾八核Coffee Lake處理器曝光:2.6GHz,16MB緩存
逍遙-《Go實現的高性能http緩存伺服器Jaguar》
spring-boot-2.0.3之redis緩存實現
緩存架構SpringBoot集成Curator實現zookeeper分散式鎖
Python + Memcached:在分散式應用程序中實現高效緩存
Python + Memcached: 在分散式應用程序中實現高效緩存
python的緩存庫:cacheout
Python+Memcached:在分散式應用程序中實現高效緩存
英特爾推出升級版Optane Memory M15緩存SSD
Spark調優的關鍵—RDD Cache緩存使用詳解
開源分散式內存緩存系統 Memcrashed 被利用發起 DDoS 放大攻擊,峰值竟達 500 Gbps
redis緩存和cookie實現Session共享
杉岩數據智能緩存技術AgileCache亮相2018 Ceph亞太峰會
10nm工藝!Intel全新架構Ice Lake首次現身:一二級緩存增大