High Memory Usage with large number of object in java

I have the below sample code which loop for 50 Million time and create objects of MyDataHolder . MyDataHolder has two variants one is hashmap based and the other is variable based. see (MyDataHolder_map and MyDataHolder_variable)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class MemTest {
public static void main(String[] args) {
List<MyDataHolder_map> list = new LinkedList<>();
for (int i = 0; i < 50_000_000; i++) {
if(i % 100_000==0) System.out.println(i);
MyDataHolder_map d = new MyDataHolder_map();
d.add("key1", "value1"+i);
d.add("key2", "value2"+i);
d.add("key3", "value3"+i);
d.add("key4", "value4"+i);
d.add("key5", "value5"+i);
d.add("key6", "value6"+i);
list.add(d);
}
}
}
// use Map to store data
class MyDataHolder_map {
private Map<String, String> data = new HashMap<>(6);
public void add(String key, String value) {
data.put(key, value);
}
}
// use variables
class MyDataHolder_variable {
private String key1;
private String key2;
private String key3;
private String key4;
private String key5;
private String key6;
public void add(String key, String value) {
switch (key) {
case "key1" -> key1 = value;
case "key2" -> key2 = value;
case "key3" -> key3 = value;
case "key4" -> key4 = value;
case "key5" -> key5 = value;
case "key6" -> key6 = value;
}
}
}
</code>
<code>public class MemTest { public static void main(String[] args) { List<MyDataHolder_map> list = new LinkedList<>(); for (int i = 0; i < 50_000_000; i++) { if(i % 100_000==0) System.out.println(i); MyDataHolder_map d = new MyDataHolder_map(); d.add("key1", "value1"+i); d.add("key2", "value2"+i); d.add("key3", "value3"+i); d.add("key4", "value4"+i); d.add("key5", "value5"+i); d.add("key6", "value6"+i); list.add(d); } } } // use Map to store data class MyDataHolder_map { private Map<String, String> data = new HashMap<>(6); public void add(String key, String value) { data.put(key, value); } } // use variables class MyDataHolder_variable { private String key1; private String key2; private String key3; private String key4; private String key5; private String key6; public void add(String key, String value) { switch (key) { case "key1" -> key1 = value; case "key2" -> key2 = value; case "key3" -> key3 = value; case "key4" -> key4 = value; case "key5" -> key5 = value; case "key6" -> key6 = value; } } } </code>
public class MemTest {
    public static void main(String[] args) {
        List<MyDataHolder_map> list = new LinkedList<>();
        for (int i = 0; i < 50_000_000; i++) {
            if(i % 100_000==0) System.out.println(i);
            MyDataHolder_map d = new MyDataHolder_map();
            d.add("key1", "value1"+i);
            d.add("key2", "value2"+i);
            d.add("key3", "value3"+i);
            d.add("key4", "value4"+i);
            d.add("key5", "value5"+i);
            d.add("key6", "value6"+i);
            list.add(d);
        }
    }
}
// use Map to store data
class MyDataHolder_map {
    private Map<String, String> data = new HashMap<>(6);

    public void add(String key, String value) {
        data.put(key, value);
    }
}

// use variables
class MyDataHolder_variable {
    private String key1;
    private String key2;
    private String key3;
    private String key4;
    private String key5;
    private String key6;

    public void add(String key, String value) {
        switch (key) {
            case "key1" -> key1 = value;
            case "key2" -> key2 = value;
            case "key3" -> key3 = value;
            case "key4" -> key4 = value;
            case "key5" -> key5 = value;
            case "key6" -> key6 = value;
        }
    }
}

I run the code with 32 GB ram -Xmx32g -verbose:gc and notice that the variable based MyDataHolder print following at the end of the program:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>With Variable based MyDataHolder
[32.495s][info][gc] GC(34) Pause Young (Normal) (G1 Evacuation Pause) 24032M->24072M(31312M) 1259.276ms
[35.537s][info][gc] GC(27) Pause Remark 24128M->24128M(32768M) 2.067ms
[59.101s][info][gc] GC(27) Pause Cleanup 24128M->24128M(32768M) 1.132ms
[59.234s][info][gc] GC(27) Concurrent Mark Cycle 39850.818ms
</code>
<code>With Variable based MyDataHolder [32.495s][info][gc] GC(34) Pause Young (Normal) (G1 Evacuation Pause) 24032M->24072M(31312M) 1259.276ms [35.537s][info][gc] GC(27) Pause Remark 24128M->24128M(32768M) 2.067ms [59.101s][info][gc] GC(27) Pause Cleanup 24128M->24128M(32768M) 1.132ms [59.234s][info][gc] GC(27) Concurrent Mark Cycle 39850.818ms </code>
With Variable based MyDataHolder
[32.495s][info][gc] GC(34) Pause Young (Normal) (G1 Evacuation Pause) 24032M->24072M(31312M) 1259.276ms
[35.537s][info][gc] GC(27) Pause Remark 24128M->24128M(32768M) 2.067ms
[59.101s][info][gc] GC(27) Pause Cleanup 24128M->24128M(32768M) 1.132ms
[59.234s][info][gc] GC(27) Concurrent Mark Cycle 39850.818ms

Notice it took 24128M i.e 24 GB RAM.However with Hashmap based “MyDataHolder` fails and it print

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[81.370s][info][gc] GC(73) Pause Young (Normal) (G1 Preventive Collection) 32581M->32583M(32768M) 42.816ms
[81.458s][info][gc] GC(74) To-space exhausted
[81.458s][info][gc] GC(74) Pause Young (Concurrent Start) (G1 Preventive Collection) 32615M->32647M(32768M) 82.420ms
[81.458s][info][gc] GC(75) Concurrent Mark Cycle
[81.519s][info][gc] GC(76) To-space exhausted
[81.519s][info][gc] GC(76) Pause Young (Normal) (G1 Preventive Collection) 32663M->32679M(32768M) 52.343ms
</code>
<code>[81.370s][info][gc] GC(73) Pause Young (Normal) (G1 Preventive Collection) 32581M->32583M(32768M) 42.816ms [81.458s][info][gc] GC(74) To-space exhausted [81.458s][info][gc] GC(74) Pause Young (Concurrent Start) (G1 Preventive Collection) 32615M->32647M(32768M) 82.420ms [81.458s][info][gc] GC(75) Concurrent Mark Cycle [81.519s][info][gc] GC(76) To-space exhausted [81.519s][info][gc] GC(76) Pause Young (Normal) (G1 Preventive Collection) 32663M->32679M(32768M) 52.343ms </code>
[81.370s][info][gc] GC(73) Pause Young (Normal) (G1 Preventive Collection) 32581M->32583M(32768M) 42.816ms
[81.458s][info][gc] GC(74) To-space exhausted
[81.458s][info][gc] GC(74) Pause Young (Concurrent Start) (G1 Preventive Collection) 32615M->32647M(32768M) 82.420ms
[81.458s][info][gc] GC(75) Concurrent Mark Cycle
[81.519s][info][gc] GC(76) To-space exhausted
[81.519s][info][gc] GC(76) Pause Young (Normal) (G1 Preventive Collection) 32663M->32679M(32768M) 52.343ms

Notice it consumed all 32 GB RAM.

Question:

  1. Why the program is taking 24GB with variable version , it should be way less as total number of unique string in VM are 6 Keys per iterator i.e 6*50M= 300M and each string is 7 byte so total is 300M * 7Byte = 1.9 GB. I understand there are overhead of references and objects but that cant be 20+ GBs. So what is going on?
  2. Why Hashmap based “MyDataHolder` need more space 32 GB as compared to variable based. Does HashMap have very high mem overheads?

PS: using java17

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật