C++和Java容器内存消耗占用比较

C++和Java容器内存消耗占用比较

码农世界 2024-05-12 前端 72 次浏览 0个评论

事情起因: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::vector sds;
	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()
{
	ArrayList data = 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

结论:

  1. C++ vector内存消耗与Java原生类型数组int[]消耗相当(C++多一点是动态扩容会多扩)
  2. Java map存储多消耗2倍多 , ArrayList比std::vector多耗4倍多, 猜测原因Java容器中只能存对象, 有额外装箱和拆箱消耗内存

转载请注明来自码农世界,本文标题:《C++和Java容器内存消耗占用比较》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,72人围观)参与讨论

还没有评论,来说两句吧...

Top