【问题标题】:How to calculate the memory consumed by hash map?如何计算 hash map 消耗的内存?
【发布时间】:2019-10-23 11:34:20
【问题描述】:

如何计算哈希图的内存。 hash map的内存是我想要得到的。

我可以通过hashmap.size();得到的hash map的大小

但我不知道如何得到这个。

【问题讨论】:

标签: android memory hashmap


【解决方案1】:

通过使用 java.io 库中的几个输出流尝试这样的事情:

import java.util.*;
import java.io.*;

HashMap<String, Integer> hashmap = new HashMap<>();
hashmap.put("vishal", 10); 
hashmap.put("sachin", 30); 
hashmap.put("vaibhav", 20);

try {
    System.out.println("HashMap Size: " + hashmap.size());
    ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
    ObjectOutputStream stream2 = new ObjectOutputStream(stream1);
    stream2.writeObject(hashmap);
    stream2.close();
    System.out.println("Memory Used: " + stream1.size());
} catch(IOException e) {
    e.printStackTrace();
}

您也可以尝试使用外部工具/API,例如 ClassMexer 或 Instrumentation 接口。

【讨论】:

    猜你喜欢
    • 2019-02-11
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多