Tomcat占用CPU高

发布于 2024-07-17 02:17:07 字数 111 浏览 4 评论 0原文

Tomcat.exe 消耗了 75% 的 CPU。 有谁知道为什么会发生这种情况以及如何减少这种情况?

我使用的是Tomcat5.5 & J2SDK v 1.4.2_12

Tomcat.exe is consuming 75% of CPU.
Is anyone having any idea why it happens and how can that be decreased?

I am using Tomcat5.5 & J2SDK v 1.4.2_12

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

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

发布评论

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

评论(10

吻安 2024-07-24 02:17:07

如果您使用 75% CPU 并且不明白为什么,我建议您向 tomcat 进程发出kill -3(如果您有控制台,则按ctrl-break)以获取线程转储(当负载很高时!)。 根据我的经验,大多数线程应该处于空闲状态或处于 io-wait 状态。 查找在堆栈跟踪中重复出现的任何单个代码分支,这可能是罪魁祸首(非 io 等待!)。 这是“穷人的分析器”,通常是解决这些问题的最佳和最有效的方法。

If you're using 75% CPU and dont understand why, I suggest you issue a kill -3 to the tomcat process (ctrl-break if you have a console) to get a thread dump (when the load is high!). In my experience most threads should either be idle or in io-wait. Look for any single branch of code that has repeated occurences in the stack traces and that's your likely culprit (non-io waits!). This is the "poor man's profiler" that is quite often the best and most efficient way to solve these problems.

一梦浮鱼 2024-07-24 02:17:07

我的日志充满了 Tomcat 日志。 我把它们全部删除了,CPU 使用率急剧下降。

My logs were full of Tomcat logs. I deleted all of them and the CPU usage has gone dramatically down.

只想待在家 2024-07-24 02:17:07

要了解发生了什么,您应该尝试在分析器下运行它。 尝试 YourKit (http://www.yourkit.com/) 或 Netbeans (http://profiler.netbeans.org/docs/help/5.5/profile_j2ee_profileproject.html )。

YourKit 与 tomcat 集成得更好。

To understand what's happening, you should try to run it under a profiler. Try the YourKit (http://www.yourkit.com/) or Netbeans (http://profiler.netbeans.org/docs/help/5.5/profile_j2ee_profileproject.html).

The YourKit one have better integration with tomcat.

安人多梦 2024-07-24 02:17:07

Lambda Probe 是一个非常方便的 Tomcat 监控工具。

您使用的是四 CPU 系统吗? Tomcat 可能在其中 3 个中运行 100%。 我首先会在应用程序中测试无限循环或类似的东西。

Lambda Probe is a very handy tool for monitoring Tomcat.

Are you using a quad CPU system? Probably Tomcat is running 100% in 3 of them. I would first test for an infinite loop or something like that in an application.

迷路的信 2024-07-24 02:17:07

首先(这适用于所有 Java 应用程序)您必须确定哪个线程正在使用 CPU。 这在 JDK 1.6 中是可能的。 它是通过使用 java.lang.management.ManagementFactory.getThreadMXBean() 来完成的。
以下是示例用法 (JSP):

<%@ page import="java.lang.management.*, java.util.*" %>
<%!
    Map cpuTimes = new HashMap();
    Map cpuTimeFetch = new HashMap();
%><%
long cpus = Runtime.getRuntime().availableProcessors();
ThreadMXBean threads = ManagementFactory.getThreadMXBean();
long now = System.currentTimeMillis();
ThreadInfo[] t = threads.dumpAllThreads(false, false);
for (int i = 0; i < t.length; i++) {
    long id = t[i].getThreadId();
    Long idid = new Long(id);
    long current = 0;
    if (cpuTimes.get(idid) != null) {
        long prev = ((Long) cpuTimes.get(idid)).longValue();
        current = threads.getThreadCpuTime(t[i].getThreadId());
        long catchTime = ((Long) cpuTimeFetch.get(idid)).longValue();
        double percent = (current - prev) / ((now - catchTime) * cpus * 10000);
        if (percent > 0 && prev > 0) {
            out.println("<li>" + t[i].getThreadName() + " " + percent + " (" + prev + ", " + current + ")");    
        }
    }
    cpuTimes.put(idid, new Long(current));  
    cpuTimeFetch.put(idid, new Long(now));
}
%>

之后,您可以获取线程转储并分析该线程中的代码以修复 CPU 使用率过高的问题。

First of all (this applies to all java applications) you must pin down which thread is using CPU. This is possible in JDK 1.6. It is done by using java.lang.management.ManagementFactory.getThreadMXBean().
Here is example usage (JSP):

<%@ page import="java.lang.management.*, java.util.*" %>
<%!
    Map cpuTimes = new HashMap();
    Map cpuTimeFetch = new HashMap();
%><%
long cpus = Runtime.getRuntime().availableProcessors();
ThreadMXBean threads = ManagementFactory.getThreadMXBean();
long now = System.currentTimeMillis();
ThreadInfo[] t = threads.dumpAllThreads(false, false);
for (int i = 0; i < t.length; i++) {
    long id = t[i].getThreadId();
    Long idid = new Long(id);
    long current = 0;
    if (cpuTimes.get(idid) != null) {
        long prev = ((Long) cpuTimes.get(idid)).longValue();
        current = threads.getThreadCpuTime(t[i].getThreadId());
        long catchTime = ((Long) cpuTimeFetch.get(idid)).longValue();
        double percent = (current - prev) / ((now - catchTime) * cpus * 10000);
        if (percent > 0 && prev > 0) {
            out.println("<li>" + t[i].getThreadName() + " " + percent + " (" + prev + ", " + current + ")");    
        }
    }
    cpuTimes.put(idid, new Long(current));  
    cpuTimeFetch.put(idid, new Long(now));
}
%>

After that you can get a thread dump and analyze the code in this thread to fix excessive CPU usage.

御守 2024-07-24 02:17:07

我们刚刚解决了一个问题,我们的 tomcat 实例运行时 CPU 使用率非常高,每隔几秒就会达到 100% 甚至更多,然后又短暂下降。 无论服务器是否正在执行任何工作,这种情况都会整天整夜发生。 我们正在使用 java 8 运行 tomcat 8。

我们在网络搜索中没有找到我们的解决方案,因此我将其发布在这里,希望对其他人有所帮助。

我们使用了 tomcat/conf/Catalina/localhost 目录中的配置选项,其中我们将 tomcat 指向另一个目录,而不是它自己的 webapps 目录。 该目录中的 xml 文件如下所示:

<?xml version='1.0'?>
<Context
docBase="/opt/dspace/amaddev/dspace-6.3/webapps/jspui"
reloadable="true"
cachingAllowed="false"/>

这有效,tomcat 使用这些目录中的代码而不是其自己的 webapps 目录运行。 然而,我们遇到了 CPU 使用率持续飙升的问题。

为了进行测试,我们从conf/Catalina/localhost目录中删除了xml文件,并重新启动了tomcat。 突然之间,我们又多了一只正常、乖巧的雄猫。 为了将 tomcat 指向另一个目录(我们在其中编译 dspace 代码),我们只需使用 conf/server.xml 中的 Host 条目,并将 appBase 设置更改为我们的 dspace 目录:

<Host name="localhost"  appBase="/opt/dspace/amaddev/dspace-6.3/webapps"
        unpackWARs="true" autoDeploy="true">

现在,这可以通过当服务器安静时,CPU 使用率非常低(低于 1%)。

We just solved a problem with our tomcat instance running with very high cpu usage, swinging up to 100% and more every few seconds and then briefly down again. This occurred all day long and all night long, whether the server was performing any work or not. We are running tomcat 8 with java 8.

We did not find our solution in a web search, so I am posting it here in the hopes of helping someone else.

We had used the configuration option in the tomcat/conf/Catalina/localhost directory, in which we pointed tomcat to another directory, other than its own webapps directory. The xml files in this directory look like this:

<?xml version='1.0'?>
<Context
docBase="/opt/dspace/amaddev/dspace-6.3/webapps/jspui"
reloadable="true"
cachingAllowed="false"/>

And this worked, tomcat ran with the code in these directories rather than its own webapps directory. However, we had this problem with continually spiking cpu usage.

To test, we removed the xml files from the conf/Catalina/localhost directory, and restarted tomcat. Suddenly we had a normal, well-behaved tomcat again. In order to point the tomcat to this other directory (where we compile our dspace code), we just used the Host entry in conf/server.xml, and changed the appBase setting to our dspace directory:

<Host name="localhost"  appBase="/opt/dspace/amaddev/dspace-6.3/webapps"
        unpackWARs="true" autoDeploy="true">

This now accomplishes what we wanted with a very low cpu usage (below 1%) when the server is quiet.

天气好吗我好吗 2024-07-24 02:17:07

所有答案都涵盖了如何进行精确诊断,此外,我想补充一点,根据我的经验,您的应用程序之一中的无限循环可能是罪魁祸首。

正如 J-16 SDiZ 所说,最好的选择是运行分析器将问题范围缩小到一个应用程序。

All the answers cover how to do an exact diagnose, in addition I would add that, from my experience, a infinite loop in one of your applications is probably the culprit.

As J-16 SDiZ said, your best bet is to run the profiler to narrow down the problem to one application.

皓月长歌 2024-07-24 02:17:07

就我而言,我刚刚使用默认设置安装了 Tomcat8。 我必须设置内存参数-Xms -Xmx。 有一次,我增加了 JVM 的内存分配,CPU 利用率急剧下降。

In my case, I had just installed Tomcat8 with default settings. I had to set memory parameters -Xms -Xmx. Once, I increased memory allocation to JVM, CPU utilization came down drastically.

巨坚强 2024-07-24 02:17:07

这很可能是由您在 tomcat 上运行的应用程序引起的。 当然,如果您的应用程序流量非常高,这也可能是原因。

This is most likely caused by the application(s) that you are running on top of the tomcat. Of course if you have very high traffic on your applications, this could also be the reason.

爱格式化 2024-07-24 02:17:07

我建议查看 Tomcat 日志文件,尤其是名为 catalina.out 的文件。
就我而言,该文件正在快速扩展,并显示错误消息“没有权限读取文件夹 /var/lib/mysql”。 我的应用程序包括一个监视文件夹 /var/lib/mysql 的监视服务。 通过允许应用程序读取此文件夹,CPU 使用率大幅下降。 系统更新后出现高 CPU 使用率,该系统更新可以更改访问文件夹和文件的权限。 因此,CPU 使用率高的原因可能是 Web 应用程序外部的,也可能是 Tomcat 容器外部的。

I recommend to look at the Tomcat log files, especially file called catalina.out.
In my case that file was expanding fast with error messages saying "no permissions to read folder /var/lib/mysql". My application includes a watch service that monitors folder /var/lib/mysql. By allowing the application to read this folder the CPU usage came down drastically. High CPU usage appeared after a system update that can change permissions to access folders and files. Thus the reason for high CPU usage can be external to the web application and also external to Tomcat container.

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