如何获取HashSet限制大小?

时间:2016-02-18 07:17:23

标签: java performance

我想在我的开发系统中获得HashSet Limit字节大小。

所以我只是添加了转储数据源代码

掠夺我的源代码

String DUMP = "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll";
void testSetLimitByte(){
    File f = new File("d:/test.txt");
    BufferedWriter bw = null;
    HashSet<String> set = new HashSet<String>();
    int cnt = 0;
    try {
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/test.txt", false) , "UTF-8"));
        for (int i = 0; i<100000000; i++) {
            String dumpData = DUMP + i;

            bw.write(dumpData);
            bw.newLine();
            if(i == 0)
                continue;

            set.add(dumpData);

            if(i%10000 == 0)
                System.out.print(".");
            if(i%100000 == 0)
                System.out.print(" ");
            if(i%1000000 == 0){
                cnt++;
                System.out.println(cnt +" (size 1billion)");
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(bw != null)
                bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("HashSet Limit Memory : " + f.length() +"bytes");
    }
}

此代码是否获得类似的HashSet限制字节大小..?

1 个答案:

答案 0 :(得分:0)

HashSet不受字节限制,只有总堆大小有限。

注意:要达到HashSet的Integer.MAX_VALUE大小,您需要~64 GB的堆(以及简单的键/值)

  

在某些系统中添加多少个HashSet字节它没有发生OutOfMemoryError

在这种情况下,您可以发现JVM只是在尝试使用内存的最后部分时运行得越来越慢。在较新的JVM中,它会检测到您正在接近这种情况并且稍早死亡但是可能需要很长时间才能达到这一点。

相关问题