web应用部署到Linux服务器上,CPU占用过高

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liuyifeng1920/article/details/52460482

web应用部署到Linux服务器上,CPU占用过高

公司项目部署到LINUX服务器上后,过了一段时间,CPU过高,开始寻找原因

有这么几步:
1.通过top命令(top之后再按shift+H可以显示线程)查看CPU使用率高的线程;
2.将这个线程号转换为16进制;
3.使用jps查看服务器的Java进程号;
4.使用jstack [进程号] 打印当前的进程堆栈;
5.从打印的信息中,找到第2步得到的线程号,看看这个线程在做什么。
不一定一次就能抓准线程状态,可以第1步时多记几个线程。

最后发现是代码中有外部远程调用,造成了线程死锁。


展开阅读全文

Socket服务占用CPU过高

07-06

using System;rnusing System.Collections.Generic;rnusing System.Text;rnusing System.Net.Sockets;rnrnnamespace DataCeneterServicernrn class SocketServerrn rn private TcpListener _TcpService;rn private string _Port;rn /// rn /// 端口号rn /// rn public string Portrn rn get return _Port; rn set _Port = value; rn rnrn private string _SocketIP;rn /// rn /// 获取或设置绑定的IP地址rn /// rn public string SocketIPrn rn get return _SocketIP; rn set _SocketIP = value; rn rn /// rn /// 收到消息的事件rn /// rn public EventHandler ReceiveComplete;rnrn private bool _StartFlag;rn /// rn /// 获取服务启动状态rn /// rn public bool StartFlagrn rn get return _StartFlag; rn rnrn #region 获取本地服务器地址rn /// rn /// 获取本地服务器地址rn /// rn /// System.Net.IPAddressrn private System.Net.IPAddress GetServerIP()rn rn if (string.IsNullOrEmpty(_SocketIP))rn rn string hostname = System.Net.Dns.GetHostName();rn System.Net.IPHostEntry iphost = System.Net.Dns.GetHostEntry(hostname);rn System.Net.IPAddress ipaddress = null;rn for (int i = 0; i < iphost.AddressList.Length; i++)rn rn string ipName = iphost.AddressList[i].ToString();rn tryrn rn Convert.ToInt64(ipName.Replace(".", "").Replace(" ", ""));rn ipaddress = iphost.AddressList[i];rn break;rn rn catch (Exception ex)rn rn LogInfo.WirteLog(ex.Message);rn rn rn return ipaddress;rn rn elsern rn System.Net.IPHostEntry iphost = System.Net.Dns.GetHostEntry(_SocketIP);rn System.Net.IPAddress ipaddress = null;rn for (int i = 0; i < iphost.AddressList.Length; i++)rn rn string ipName = iphost.AddressList[i].ToString();rn tryrn rn if (ipName == _SocketIP)rn rn ipaddress = iphost.AddressList[i];rn break;rn rn rn catch (Exception ex)rn rn LogInfo.WirteLog(ex.Message);rn rn rn return ipaddress;rn rn rn #endregionrnrn #region 初始化TCPIP服务rn private void InitSocketService()rn rn _TcpService = new TcpListener(GetServerIP(), Convert.ToInt32(_Port));rn rn #endregionrnrn #region 启动Socket服务rn /// rn /// 启动服务rn /// rn public void Start()rn rn tryrn rn InitSocketService();rn _TcpService.Start();rn _StartFlag = true;rn _TcpService.BeginAcceptTcpClient(new AsyncCallback(TCPAccept), _TcpService);rn rn catch (Exception ex)rn rn _StartFlag = false;rn LogInfo.WirteLog(ex.Message);rn throw ex;rn rn rn #endregionrnrn #region Socket监听rn private void TCPAccept(IAsyncResult ia)rn rn TcpListener tcpListen = ia.AsyncState as TcpListener;rn tryrn rn TcpReceive tcpreceive = new TcpReceive();rn tcpreceive.NetWork = tcpListen.EndAcceptTcpClient(ia);rn Receive(tcpreceive);rn tcpListen.BeginAcceptTcpClient(new AsyncCallback(TCPAccept), tcpListen);rn rn catch (Exception ex)rn rn _StartFlag = false;rn tcpListen.Stop();rn rn rn #endregionrnrn #region 接收Socket消息rn private void Receive(TcpReceive tcpReceive)rn rn tcpReceive.NetWork.GetStream().BeginRead(tcpReceive.Buffer, 0, tcpReceive.Buffer.Length, new AsyncCallback(ReceiveCallback), tcpReceive);rn rn #endregionrnrn #region 读取并发送消息rn private void ReceiveCallback(IAsyncResult ia)rn rn tryrn rn TcpReceive tcpReceive = ia.AsyncState as TcpReceive;rn if (_StartFlag)rn rn if (tcpReceive != null)rn rn ReceiveComplete(ia, new EventArgs());rn tcpReceive.NetWork.GetStream().BeginRead(tcpReceive.Buffer, 0, tcpReceive.Buffer.Length, new AsyncCallback(ReceiveCallback), tcpReceive);rn rn rn elsern rn if (tcpReceive != null)rn rn tcpReceive.NetWork.Client.Close();rn rn rn rn catch (Exception ex)rn rn LogInfo.WirteLog(ex.Message);rn rn rn #endregionrnrn #region 向Socket写消息rn private void SendCallback(IAsyncResult ia)rn rn TcpClient tcpClient = ia.AsyncState as TcpClient;rn tryrn rn tcpClient.GetStream().EndWrite(ia);rn rn catch (Exception ex)rn rn tcpClient.Close();rn rn rn #endregionrnrn #region 发送消息rn /// rn /// 发送消息rn /// rn /// 连接的客户端rn /// 发送的字节数据rn public void Send(TcpClient tcpClient, byte[] buffer)rn rn if (tcpClient.Connected)rn rn tcpClient.GetStream().BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(SendCallback), tcpClient);rn rn elsern rn tcpClient.Close();rn rn rn #endregionrnrn #region 关闭服务rn /// rn /// 关闭服务rn /// rn public void Close()rn rn _TcpService.Stop();rn _StartFlag = false;rn LogInfo.WirteLog("关闭服务");rn rn #endregionrn rnrn写了一个Socket服务端,但启动以后,占用CPU率达到40,如何优化 论坛

sybase占用CPU内存过高

05-14

ptsj1:~ # toprntop - 18:04:56 up 25 days, 8:59, 2 users, load average: 2.03, 2.10, 2.17rnTasks: 456 total, 3 running, 452 sleeping, 1 stopped, 0 zombiern%Cpu(s): 3.8 us, 1.2 sy, 0.0 ni, 94.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 strnKiB Mem: 32886624 total, 9221892 used, 23664732 free, 908 buffersrnKiB Swap: 33551356 total, 0 used, 33551356 free. 5415856 cached Memrn PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMANDrn30037 sybase 20 0 22124 4016 1056 R 100.00 0.012 3954:03 [atd] rn29983 sybase 20 0 22124 3844 888 R 100.00 0.012 3979:10 [atd] rnrnrnptsj1:~ # cd /proc/30037rnptsj1:/proc/30037 # llrntotal 0rndr-xr-xr-x 2 sybase users 0 May 14 17:36 attrrn-r-------- 1 sybase users 0 May 14 17:36 auxvrn-r--r--r-- 1 sybase users 0 May 14 17:36 cgrouprn--w------- 1 sybase users 0 May 14 17:36 clear_refsrn-r--r--r-- 1 sybase users 0 May 11 23:49 cmdlinern-rw-r--r-- 1 sybase users 0 May 14 17:36 commrn-rw-r--r-- 1 sybase users 0 May 14 17:36 coredump_filterrn-r--r--r-- 1 sybase users 0 May 14 17:36 cpusetrnlrwxrwxrwx 1 sybase users 0 May 14 17:36 cwd -> /tmprn-r-------- 1 sybase users 0 May 14 17:36 environrnlrwxrwxrwx 1 sybase users 0 May 12 00:30 exe -> /usr/bin/perlrndr-x------ 2 sybase users 0 May 11 23:49 fdrndr-x------ 2 sybase users 0 May 11 23:49 fdinforn-rw-r--r-- 1 sybase users 0 May 14 17:36 gid_maprn-r-------- 1 sybase users 0 May 14 17:36 iorn-r-------- 1 sybase users 0 May 14 17:36 kgr_in_progressrn-r--r--r-- 1 sybase users 0 May 14 17:36 latencyrn-r--r--r-- 1 sybase users 0 May 14 17:36 limitsrn-rw-r--r-- 1 sybase users 0 May 14 17:36 loginuidrn-r--r--r-- 1 sybase users 0 May 14 17:36 mapsrn-rw------- 1 sybase users 0 May 14 17:36 memrn-r--r--r-- 1 sybase users 0 May 14 17:36 mountinforn-r--r--r-- 1 sybase users 0 May 14 17:36 mountsrn-r-------- 1 sybase users 0 May 14 17:36 mountstatsrndr-xr-xr-x 6 sybase users 0 May 14 17:36 netrndr-x--x--x 2 sybase users 0 May 14 17:36 nsrn-r--r--r-- 1 sybase users 0 May 14 17:36 numa_mapsrn-rw-r--r-- 1 sybase users 0 May 14 17:36 oom_adjrn-r--r--r-- 1 sybase users 0 May 14 17:36 oom_scorern-rw-r--r-- 1 sybase users 0 May 14 17:36 oom_score_adjrn-r--r--r-- 1 sybase users 0 May 14 17:36 pagemaprn-r--r--r-- 1 sybase users 0 May 14 17:36 personalityrn-rw-r--r-- 1 sybase users 0 May 14 17:36 projid_maprnlrwxrwxrwx 1 sybase users 0 May 14 17:36 root -> / 论坛

没有更多推荐了,返回首页