用tomcat中的所有httpsess在任意时间点测量记忆消耗

发布于 2025-02-03 03:06:07 字数 1016 浏览 2 评论 0 原文

我想测量Web应用程序中 httpsession 使用的内存百分比。

有什么方法可以在任意时间点运行的tomcat实例中使​​用所有 httpsession s测量内存消耗,而无需修改应用程序?

我在tomcat中尝试的是

  • httpsession 的具体类是 org.apache.catalina.session.session.standardsessession 。我已经使用VisualVM介绍了该应用程序,并指定 org.apache.catalina.session.session.standardsession in [Profiler] - [Memory Settings] 。但是它仅显示标准本身的大小( contrenthashmap 的大小不包括标准中包含的大小)。
  • 我已经用飞行记录员介绍了该应用程序,并通过任务控制查看了结果。但是我找不到从 httpsession 引用哪些对象。
  • 您可以使用 org.apache.catalina.catalina.session.session.managerbase.managerbase#findsessions()代码>并用Byteman和 sizeof.deepsizeof() 测量 httpsession s的大小。但是,仅当创建新的 httpsession 时,此BYTEMAN规则才能运行。我希望在任意时间点(例如每30秒内)使用 httpsession s测量内存消耗。

I want to measure the percentage of memory used by HttpSession in a web application.

Is there any way to measure memory consumption with all HttpSessions in a running Tomcat instance at an arbitrary point in time without modifying the application?

What I have tried

  • In Tomcat, the concrete class of HttpSession is org.apache.catalina.session.StandardSession. I have profiled the application with VisualVM and specified org.apache.catalina.session.StandardSession in [Profiler]-[Memory settings]. But it shows only the size of StandardSession itself (the size of ConcurrentHashMap contained in StandardSession is not included).
  • I have profiled the application with Flight Recorder and viewed the result with Mission Control. But I cannot find out which objects are referenced from HttpSession.
  • You can list all HttpSession with org.apache.catalina.session.ManagerBase#findSessions() and measure the size of HttpSessions with Byteman and SizeOf.deepSizeOf(). But this Byteman rule runs only when a new HttpSession is created. I want measure memory consumption with HttpSessions at an arbitrary point in time (e.g. in every 30 seconds).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-02-10 03:06:07

根据@LMC的评论,我做了一个示例项目,以使用JMX获得httpsession的大小:

请注意,因为您无法访问 org.apache.catalina.session.session.managerbase ,因此此项目使用 byteman 。将 ManagerBase 将对象存储到MBEAN sessionmonitor 的静态字段中,并使用Byteman类似:

RULE Set ManagerBase object to MBean
CLASS ^org.apache.catalina.session.ManagerBase
METHOD initInternal
AT EXIT
IF TRUE
DO
    traceln("SETTING SessionManager");
    com.example.sessionsize.SessionMonitor.setSessionManager($this);
ENDRULE

并在JMX接口中使用它,例如:

package com.example.sessionsize;

import org.apache.catalina.Manager;
import org.apache.catalina.Session;

import net.sourceforge.sizeof.SizeOf;

public class SessionMonitor implements SessionMonitorMBean {

    private static Manager sessionManager = null;

    public static void setSessionManager(Manager manager) {
        sessionManager = manager;
    }

    @Override
    public long getMemoryConsumption() {
        if (sessionManager != null) {
            try {
                Session [] sessions = sessionManager.findSessions();
                return SizeOf.deepSizeOf(sessions);
            } catch(RuntimeException e) {
                // Falied to get size of HttpSession object
                e.printStackTrace();
                return -2;
            }
        } else {
            // SessionManager is not ready
            return -1;
        }
    }

    @Override
    public long getNumberOfActiveHttpSession() {
        return sessionManager.findSessions().length;
    }
}

According to the comment from @LMC, I made a sample project to get the size of HttpSession with JMX: https://github.com/satob/SessionSize

Note that because you cannot access org.apache.catalina.session.ManagerBase directly, this project uses ByteMan. Store the ManagerBase object to a static field of the MBean SessionMonitor with Byteman like:

RULE Set ManagerBase object to MBean
CLASS ^org.apache.catalina.session.ManagerBase
METHOD initInternal
AT EXIT
IF TRUE
DO
    traceln("SETTING SessionManager");
    com.example.sessionsize.SessionMonitor.setSessionManager($this);
ENDRULE

and use it in the JMX interface like:

package com.example.sessionsize;

import org.apache.catalina.Manager;
import org.apache.catalina.Session;

import net.sourceforge.sizeof.SizeOf;

public class SessionMonitor implements SessionMonitorMBean {

    private static Manager sessionManager = null;

    public static void setSessionManager(Manager manager) {
        sessionManager = manager;
    }

    @Override
    public long getMemoryConsumption() {
        if (sessionManager != null) {
            try {
                Session [] sessions = sessionManager.findSessions();
                return SizeOf.deepSizeOf(sessions);
            } catch(RuntimeException e) {
                // Falied to get size of HttpSession object
                e.printStackTrace();
                return -2;
            }
        } else {
            // SessionManager is not ready
            return -1;
        }
    }

    @Override
    public long getNumberOfActiveHttpSession() {
        return sessionManager.findSessions().length;
    }
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文