java 测量 HttpSession 对象的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1633664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measuring the Size of HttpSession object
提问by Langali
Is there a clean and easy way to measure it other than programatically measuring the size of each data type (that each object is composed of) stored in session?
除了以编程方式测量存储在会话中的每种数据类型(每个对象由其组成)的大小之外,是否有一种干净且简单的方法来测量它?
回答by Vineet Reynolds
MessAdminallows you to compute the HttpSession size although it is unclear on how it calculates the size of transient objects.
MessAdmin允许您计算 HttpSession 大小,尽管它如何计算瞬态对象的大小尚不清楚。
It appears that getting an approximate size of the HttpSession object is an exercise in futility in production, and one is likely to get a more accurate size for a controlled environment.
看起来获取 HttpSession 对象的近似大小在生产中是徒劳的,并且可能会为受控环境获得更准确的大小。
One thing to note is that size of the serialized session object is bound to inaccurate due to changes in character encoding - Strings in Java are stored in the UTF-16 format whereas the output stream could be in a different encoding. More details on why calculating the size of an object in Java is a problem, can be found in this JavaWorld article.
需要注意的一件事是,由于字符编码的变化,序列化会话对象的大小必然不准确——Java 中的字符串以 UTF-16 格式存储,而输出流可能采用不同的编码。关于为什么在 Java 中计算对象的大小是一个问题的更多详细信息,可以在这篇JavaWorld 文章中找到。
回答by kafuchau
There's a SessionSize class herethat has a function to return the size of an HttpSession object passed to it, a part of the Java Web Partslib.
这里有一个 SessionSize 类,它有一个函数来返回传递给它的 HttpSession 对象的大小,它是Java Web 部件库的一部分。
回答by Vladimir Dyuzhev
Session content must be Serializable. So serialize it and see the size of the resulting byte array.
会话内容必须是可序列化的。因此将其序列化并查看生成的字节数组的大小。
It's not equal to the in-memory size, but can be used as a rough representation of it.
它不等于内存大小,但可以用作它的粗略表示。
P.S. Note that transient fields, if any, will be excluded.
PS 请注意,临时字段(如果有)将被排除在外。

