事情起因:Java端占用内存高,将一堆数据表属性map存起来来,同样的属数据load进Java内存和
C++内存大小差几倍,遂做如下测试:
测试c++ vector,map 与Java List,Map内存占用比较:
C++代码:
测试c++ vector,map 与Java List,Map内存占用比较 C++GS_TEST(LoadMemoryTest2, LoadMemoryTest, LoadMemoryTest, 20240429) { std::vectorsds; for (int i = 0; i < 10000000; i++) sds.push_back(i); std::cout << sds.size() << "--" << std::endl; } GS_TEST(LoadMemoryTest3, LoadMemoryTest, LoadMemoryTest, 20240429) { std::map attributes; std::vector sds; for (int i = 0; i < 10000000; i++) attributes[GsStringHelp::ToString(i).c_str()] = GsStringHelp::ToString(i).c_str(); std::cout << sds.size() << "--" << std::endl; } GS_TEST(LoadMemoryTest4, LoadMemoryTest, LoadMemoryTest, 20240429) { std::map attributes; std::vector sds; for (int i = 0; i < 10000000; i++) attributes[GsStringHelp::ToString(i).c_str()] = GsAny(i); std::cout << sds.size() << "--" << std::endl; }
Java:
public static void testarray() { ArrayListdata = new ArrayList<>(); for (int i = 0; i < 10000000; i++) data.add(i); System.console().printf(""+data.size()); } public static void testarray2() { int[] data = new int[10000000]; for (int i = 0; i < 10000000; i++) data[i]=i; System.console().printf(""+data.length); } public static void testarrayLinkedList() { LinkedList data = new LinkedList<>(); for (int i = 0; i < 10000000; i++) data.add(i); System.console().printf(""+data.size()); } public static void testarrayVector() { Vector data = new Vector<>(); for (int i = 0; i < 10000000; i++) data.add(i); System.console().printf(""+data.size()); } public static void testarray3HashMap() { HashMap attributes = new HashMap<>(); for (int i = 0; i < 10000000; i++) { attributes.put( String.valueOf(i), String.valueOf(i)); } System.console().printf(""+attributes.size()); } public static void testarray3LinkedHashMap() { LinkedHashMap attributes = new LinkedHashMap<>(); for (int i = 0; i < 10000000; i++) { attributes.put( String.valueOf(i), String.valueOf(i)); } System.console().printf(""+attributes.size()); } public static void testarray3TreeMap() { TreeMap attributes = new TreeMap<>(); for (int i = 0; i < 10000000; i++) { attributes.put( String.valueOf(i), String.valueOf(i)); } System.console().printf(""+attributes.size()); } public static void testarray3HashMapany() { HashMap attributes = new HashMap<>(); for (int i = 0; i < 10000000; i++) { attributes.put( String.valueOf(i), new GsAny(i)); } System.console().printf(""+attributes.size()); }
最终比试结果统计:
语言 | 工作集内存(起始->完成) | 峰值内存(起始->完成) |
Java(ArrayList | 31M->328M | 34M->328M |
Java(int[]) | 31M->70M | 34M->70M |
Java(LinkedList | 34M->584M | 34M-584M |
Java(Vector | 34M->290M | 34M->290M |
Java(LinkedHashMap | 34M->2365M | 34M->2365M |
Java(TreeMap | 34M->2002M | 34M->2002M |
Java(HashMap | 31M->2112M | 34M->2112M |
Javav(HashMap | 45M->2593M | 45M->2593M |
C++(std::vector | 19M->59M | 23M->82M |
C++(std::map | 19M->1116M | 23M->1116M |
C++(std::map | 19M->959M | 23M->959M |
结论:
- C++ vector
内存消耗与Java原生类型数组int[]消耗相当(C++多一点是动态扩容会多扩) - Java map存储多消耗2倍多 , ArrayList比std::vector多耗4倍多, 猜测原因Java容器中只能存对象, 有额外装箱和拆箱消耗内存
还没有评论,来说两句吧...