【问题标题】:How to measure HTTP-Session Size?如何测量 HTTP 会话大小?
【发布时间】:2022-02-12 00:56:22
【问题描述】:

是否有一些有效且准确的方法来跟踪基于 servlet 的应用程序中特定会话的大小?

【问题讨论】:

    标签: session tomcat


    【解决方案1】:

    Java 没有像 C 那样的 sizeof() 方法(有关更多信息,请参阅 this post),因此您通常无法在 Java 中获得 anything 的大小。但是,您可以使用HttpSessionAttributeListener(链接为 JavaEE 8 及以下版本)跟踪会话中进入和删除的内容。这将使您对属性的数量以及在一定程度上正在使用的内存量有一些了解。比如:

    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    
    @WebListener
    public class MySessionAttributeListener implements HttpSessionAttributeListener {
        
        @Override
        public void attributeAdded(HttpSessionBindingEvent event) {
            System.out.println( "the attribute \"" + event.getName() + "\" with the value \"" + event.getValue() + "\" has been added" );
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent event) {
            System.our.println( "the attribute \"" + event.getName() + "\" with the value \"" + event.getValue() + "\" has been removed" );     
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent event) {
            System.out.println( "the attribute \"" + event.getName() + "\" with the value \"" + event.getValue() + "\" has been replaced" );
        }
    }
    

    【讨论】:

    • 您还可以将会话序列化为字节(或磁盘上)并查看输出有多大。这不会直接映射到内存中的对象大小,但会在同一个范围内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2017-10-16
    • 2016-10-22
    • 2023-02-21
    • 2016-11-26
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多