简体   繁体   English

Java Web应用程序真的很慢

[英]Java web application really slow

I am using Rackspace as a hosting provider, using their Cloud server hosting, with 256mb plan. 我使用Rackspace作为托管服务提供商,并使用其256mb计划的Cloud服务器托管。

I am using Geronimo 2.2 to run my java application. 我正在使用Geronimo 2.2运行我的Java应用程序。

The server starts up no problem, loads Geronimo quite fast, however, when I started to deploy my web application, it is taking forever, and once it is deployed, it takes forever to navigate through pages. 服务器启动正常,加载Geronimo的速度非常快,但是,当我开始部署Web应用程序时,它会花费很多时间,而一旦部署它,就需要花费很多时间浏览页面。

I've been monitoring the server activity, the CPU is not so busy, however, 60% of the memory is being used up. 我一直在监视服务器的活动,CPU并不是很忙,但是60%的内存已用完。 Could this be the problem? 这可能是问题吗?

If so, what are my options? 如果是这样,我有什么选择? Should I consider upgrading this cloud server to something with more RAM, or changing a host provider to better suit my needs? 我应该考虑将此云服务器升级到具有更多RAM的设备,还是更改主机提供商以更好地满足我的需求?


Edit: I should note that, even if I don't deploy my application, just having Geronimo loaded, sometimes I would get a connection time when I try to shut down Geronimo. 编辑:我应该注意,即使我不部署我的应用程序,只是加载了Geronimo,有时当我尝试关闭Geronimo时我也会得到一个连接时间。

Also the database is on the same server as the application. 此外,数据库与应用程序位于同一服务器上。 (however I wouldn't say its query intensive) (但是我不会说它查询密集)


Update : 更新
After what @matiu suggested, I tried running free -m, and this is the output that I get: 在@matiu建议之后,我尝试运行free -m,这是我得到的输出:

             total       used       free     shared    buffers     cached
Mem:           239        232          6          0          0          2
-/+ buffers/cache:        229          9
Swap:          509        403        106

This was totally different result than running ps ux , which is how I got my previous 60%. 这与运行ps ux完全不同,这就是我之前获得60%的结果。

And I did an iostat check, and about 25% iowait time, and device is constantly writing and reading. 我进行了iostat检查,大约有25%的iowait时间,并且设备一直在读写。


update: 更新:
Upgraded my hosting to 512MB, now it is up to speed! 将我的主机升级到512MB,现在可以加快速度! Something I should note is, I forgot about the Java's Permanent Generation memory, which is also used by Geronimo. 我应该注意的是,我忘记了Java的永久生成内存,Geronimo也使用了该内存。 So it turns out, I do need more RAM, and more RAM did solve my problem. 事实证明,我确实需要更多RAM,而更多RAM确实解决了我的问题。 (As expected) yay. (如预期)

I'm guessing you're running into 'swapping'. 我猜您正在遇到“交换”。

As you'll know Linux swaps out some memory to disk. 如您所知,Linux会将一些内存换成磁盘。 This is great for memory that's not accessed very much. 这对于不太需要访问的内存非常有用。

When Java starts eating heaps and heaps, linux starts: 当Java开始吃堆时,Linux启动:

  1. Swapping memory block A out to disk to make space to read in block B 将内存块A交换到磁盘上以腾出空间以读取块B
  2. Reading/writing block B 读/写块B
  3. Swapping block B to disk to make space for some other block. 将块B交换到磁盘以为其他块腾出空间。

As disk is 1000s of times slower than RAM, as the memory usage increases your machine grinds more and more closer to a halt. 由于磁盘的速度比RAM慢1000倍,因此随着内存使用量的增加,您的计算机变得越来越停顿。

With 256 MB Cloud Servers you get 512 MB of Swap space. 使用256 MB的云服务器,您可以获得512 MB的交换空间。


Checking: 检查:

You can check if this is the case with free -m .. this page shows how to read the output: 您可以使用free -m检查这种情况。 此页面显示了如何读取输出:

Next I'd check with 'iostat 5' to see what the disk IO rate on the swap partition is. 接下来,我将使用“ iostat 5”进行检查,以查看交换分区上的磁盘IO速率是多少。 I would say a write rate of 300 or more means you're almost dead in the water. 我想说300或更高的写入率意味着您几乎要死在水里了。 I'd say you'd want to keep the write rate of the swap partition down below 50 blocks a second and the read rate down below 500 blocks a second .. if possible both should be zero most of the time. 我要说的是,您希望将交换分区的写入速率降低到每秒50块以下,将读取速率降低到每秒500块以下..如果可能的话,大多数时候两者都应为零。 Remember disk is 1000s of times slower than RAM. 请记住,磁盘要比RAM慢1000倍。

You can check if it's Java eating the ram by running top and hitting shift+m to order the processes by memory consumption. 您可以通过运行top并按shift+m来按内存消耗对进程进行排序来检查Java是否在吃掉内存。

If you want .. you can disable the swap partition with swapoff -a .. then open up the web console, and hit the site a bit .. you'll soon see error messages in the console like 'OOM Killed process xxx' (OOM is for Out of Memory I think). 如果您想要..您可以使用swapoff -a ..禁用交换分区,然后打开Web控制台,并稍稍访问该站点..您很快就会在控制台中看到错误消息,例如“ OOM Killed process xxx”(OOM已杀死进程xxx)( OOM是为内存不足而设计的)。 If you see those that's linux trying to satisfy memory requests by killing processes. 如果您看到那些Linux试图通过杀死进程来满足内存请求。 Once that happens, it's best to hard reboot. 一旦发生这种情况,最好进行硬重启。


Fixing: 定影:

If it's Java using the RAM .. this link might help. 如果是使用RAM的Java,则此链接可能会有所帮助。

I think the easy fix would be just to upgrade the size of the Cloud Server. 我认为简单的解决方法只是升级Cloud Server的大小。

You may find a different Java RTE may be better. 您可能会发现不同的Java RTE可能更好。

If you run it in a 32 bit chroot it may use less RAM. 如果在32位chroot中运行它,则可能会使用更少的RAM。

You should consider running a virtual dedicated Linux server, from something like linode. 您应该考虑从诸如linode之类的设备运行虚拟专用Linux服务器。 You'd have to worry about how to start a Java service and things like firewalls, etc, but once you get it right, you are in effect you're own hosting provider, allowing you to do anything a standalone actual Linux box can do. 您将不得不担心如何启动Java服务以及诸如防火墙之类的事情,但是一旦正确使用,您实际上就是您自己的托管服务提供商,允许您执行独立的实际Linux机器可以做的任何事情。

As for memory, I wouldn't upgrade until you have evidence that you do not have enough. 至于内存,在您没有足够的证据之前,我不会升级。 60% being used up is less than 100% used up... 60%的用完少于100%的用完...

Java normally assumes that it can take whatever it is assigned to it. Java通常假定它可以接受分配给它的任何内容。 Meaning, if you give it a max of 200MB, it thins that it's ok to take 200MB even though it's using much less. 意思是,如果您为其提供最大200MB的内存,那么即使它使用更少的内存,它也可以占用200MB的内存。 There is a way to make Java use less memory, by using the -Xincgc incremental garbage collector. 通过使用-Xincgc增量垃圾收集器,可以使Java使用更少的内存。 It actually ends up giving chunks of memory back to the system when it no longer needs it. 实际上,它最终在不再需要内存时将其提供给系统。 This is a bit of a kept secret really. 这确实是个秘密。 You won't see anyone point this out... 您不会看到有人指出这一点...

Based on my experience, memory and CPU load on VPSes are quite related. 根据我的经验,VPS上的内存和CPU负载是非常相关的。 Meaning, when application server will take up all available memory, CPU usage starts to sky rock, finally making application inaccessible. 这意味着,当应用程序服务器将占用所有可用内存时,CPU使用率开始飞速上涨,最终使应用程序无法访问。

This is just a side effect though - you should really need to investigate where your problems origin! 不过,这只是一个副作用-您确实需要调查问题的出处!

If the memory consumption is very high, then you can have multiple causes: 如果内存消耗很高,则可能有多种原因:

  1. It's normal - maybe you have reached a point, where all processes (application server, applications within it, background processes, daemons, Operating System, etc.) put together need that huge amount of memory. 这很正常-也许您已经达到了一个目的,所有进程(应用程序服务器,其中的应用程序,后台进程,守护程序,操作系统等)加在一起需要大量内存。 This is least probably scenario . 这是最不可能的情况
  2. Memory leak - can happen due to bug in framework or some library (not likely) or your own code (possible). 内存泄漏-可能由于框架或某些库中的错误(不太可能)或您自己的代码(可能)而发生。 This can be monitored and solved . 这可以监视和解决
  3. Huge amount of requests - each request will take both CPU and memory to be processed. 大量请求-每个请求都会占用CPU和内存。 You can have a look at the correlation between requests per second and memory consumption, meaning, it can be monitored and resolved. 您可以查看每秒请求与内存消耗之间的相关性,这意味着可以对其进行监控和解决。

If you are interested in CPU usage: 如果您对CPU使用率感兴趣:

  1. Again, monitor requests to your application. 同样,监视对您​​的应用程序的请求。 For constant count of requests - nothing extraordinary should happen. 对于不断增加的请求-不会发生任何异常情况。
  2. One component is exhausting most resources (maybe your database is installed on the same server and it uses all CPU power due to inefficient queries? Slow log would help.) 一个组件正在消耗大部分资源(也许您的数据库安装在同一服务器上,并且由于查询效率低而使用了所有CPU能力吗?慢日志将有所帮助。)

As you can see, it's not trivial task, but you have tools support which will can help you out. 如您所见,这并不是一件容易的事,但是您拥有可以帮助您的工具支持。 I personally use java melody and probe . 我个人使用Java melodyprobe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM