【发布时间】:2022-02-12 00:56:22
【问题描述】:
是否有一些有效且准确的方法来跟踪基于 servlet 的应用程序中特定会话的大小?
【问题讨论】:
是否有一些有效且准确的方法来跟踪基于 servlet 的应用程序中特定会话的大小?
【问题讨论】:
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" );
}
}
【讨论】: