之前的java测试代码 性能测试

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class 测试代码2 {

    public static void main(String args[]) {
        int intSize = 0;
        try {
          //for (int i = 191059999; i <= 191160000; i++) {
            //内存开到3个g可以跑4开头的9位数int
            for (int i = 447483646; i <= 2147483647; i++) {
                intSize = i;
                int[] allInt = new int[i];
                System.out.println("allInt:" + allInt.length);
            }
        } catch (Exception e) {
        } finally {
            System.out.println("intSize:" + intSize);
        }
    }
}
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class 测试代码1 {

    static ArrayList<Integer> dataList = new ArrayList<Integer>();

    public static void main(String args[]) {
        int judgeListmaxCount = 0;
        try {
            for (int i = 0; i <= 500000000; i++) {
                judgeListmaxCount = i;
                dataList.add(i);
            }
        } catch (Exception e) {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);
        } finally {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);

        }
    }
}
  java代码测试集合,数组等最大存储大小 

 分类: java常用知识 
2011-11-21 19:00 305人阅读 评论(0) 收藏 举报 

           其实java里面的集合,数组等他们的存储容量我们是可以预估的。1、存储原始类型,这种存储大小是固定的。2、存储对象,由于内容大小有变化,但是我们可以通过测试获取到大慨存储容量。

           还有就是我们扩容内存,会影响集合或数组存储大小。

1、List<Integer>

 int judgeListmaxCount = 0;
         try {
             for (int i = 0; i <= 50000000; i++) {
                 judgeListmaxCount = i;
                 dataList.add(i);
             }
         } catch (Exception e) {
             System.out.println("judgeListmaxCount:" + judgeListmaxCount);
         } finally {
             System.out.println("judgeListmaxCount:" + judgeListmaxCount);

        }

异常消息:

Exception in thread "main" judgeListmaxCount:38647475
 java.lang.OutOfMemoryError: Java heap space
     at java.util.ArrayList.ensureCapacity(ArrayList.java:169)
     at java.util.ArrayList.add(ArrayList.java:351)
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:21)

xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms1024m -Xmx1024m com.yxie.test.CreateDichotoSearchData
 judgeListmaxCount:38647475
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     at java.util.Arrays.copyOf(Arrays.java:2760)
     at java.util.Arrays.copyOf(Arrays.java:2734)
     at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
     at java.util.ArrayList.add(ArrayList.java:351)
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:21)

 2、int[]

int intSize = 0;
         try {
             for (int i = 191059999; i <= 191160000; i++) {
                 intSize = i;
                 int[] allInt = new int[i];
                 System.out.println("allInt:" + allInt.length);
             }
         } catch (Exception e) {
         } finally {
             System.out.println("intSize:" + intSize);
         }

异常消息:

intSize:191060000
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:54)

改变java虚拟机内存大小,会影响int[]数组大小。

xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms1024m -Xmx1024m com.yxie.test.CreateDichotoSearchData
 Integer max:2147483647
 fileSize:4000000000
 spareFileSize:1852516353
intSize:191059999
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:54)




 xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ 
xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms2024m -Xmx2024m com.yxie.test.CreateDichotoSearchData
 Integer max:2147483647
 fileSize:4000000000
 spareFileSize:1852516353
 allInt:191059999
 allInt:191060000
 allInt:191060001


import java.util.HashMap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class hashmap {

    static HashMap<Integer, Integer> dataList = new HashMap<Integer, Integer>();

    public static void main(String args[]) {
        int judgeListmaxCount = 0;
        try {
            for(int j=0;j<1000;j++){
                            //447483646
                //小于300*225000时效率较高 
            for (int i = 0; i <= 225000; i++) {
                judgeListmaxCount ++;
                dataList.put(i*j+i, i*j);}
                System.out.println(j);
            }
        } catch (Exception e) {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);
        } finally {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);

        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章