本文介绍了特定对象列表的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的pojos(一个用户类)列表,其中包含约15个简单的字段& 1个arrayList.这些代表用户&可能会将其中的100或1000个存储在内存中,以避免每次都从外部系统检索它们. (我正在使用Ehcache)

i have a list of simples pojos (a User class) with about 15 simple fields & 1 arrayList.Those represent users & maybe 100 or 1000 of them will be store in memory in order to avoid to retrieve them from external system every time. (i'm using Ehcache)

我想通过junit测试知道K个用户列表使用了多少内存.我的直觉是,即使是一千个这样的简单pojo也不会以任何方式威胁(换言之,小于100 Ko)

I would like to know with a junit test how much memory is used by a list of K of those users.I have the intuition that simple pojo like those one even for a 1000 ones are not threatening in any way (in other words less than 100 Ko)

预先感谢您的回答.我非常感谢您的帮助.

Thanks in advance for your anwser.i really appreciate your help.

推荐答案

您可以在创建对象之前和之后计算JRE所使用的内存,以便近似被您的对象使用.

You can calculate the memory used by the JRE before and after you create your object, in order to approximate how many bytes are being used by your object.

System.gc();
System.runFinalization();
Thread.sleep(1000);
long before = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

//build object here

System.gc();
System.runFinalization();
Thread.sleep(1000);
long after = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

long objectSize = after - before;

这篇关于特定对象列表的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:09