Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

Nemo

关注TA

路漫漫其修远兮,吾将上下而求索。

  •  普罗旺斯
  • 负责帅就完事了
  • 写了1,495,102字

该文章投稿至Nemo社区   数据库  板块 复制链接


Mysql 内存占用过高问题处理

发布于 2018/12/07 11:22 19,098浏览 0回复 23,453

ac6eddc451da81cb037c289d5366d016082431c3.jpg

自从上月末LinkNemo迁移到新架构后,服务器的内存偶尔不够用,尝试调配了一些程序的jvm参数,效果稍有改善,但是还不是特别理想。主要原因还是服务器的内存太小了。

第一个版本的LinkNemo程序上线的时候是在15年12月31,那时候LinkNemo服务器的内存配置才512M。后来到17年上半年,陆陆续续在LinkNemo上加了不少新功能,512内存不太够用了,这才升级到1G内存。

最近这次大版本升级主要把程序的架构迁移到了Nemo社区那一套上,主体架构用得是Springboot,占用内存比旧版本的LinkNemo架构使用的SpringMvc要稍多一些。

所以这些天一直在调试LinkNemo后端服务的JVM参数。相对刚上线的时候几个小时服务就因内存不足而卡顿,现在基本能够稳定运行。不过内存的问题还是比较紧张的。

$ top
top - 18:40:54 up  8:21,  2 users,  load average: 0.16, 0.10, 0.17
Tasks:  92 total,   1 running,  91 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.4 us,  1.4 sy,  0.0 ni,  0.0 id, 95.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1016020 total,    67364 free,   808772 used,   139884 buff/cache
KiB Swap:   524284 total,   524284 free,        0 used.    57412 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 2573 root      20   0 2282416 514712   5072 S  0.0 50.7   2:20.84 java
 2925 mysql     20   0 1083052 256628   9556 S  6.0 25.3   0:01.46 mysqld
    1 root      20   0   37972   4060   2012 S  0.0  0.4   0:01.55 systemd
  879 www-data  20   0  127992   4032   1920 S  0.0  0.4   0:11.65 nginx
 1091 root      20   0  124624   3908      0 S  0.7  0.4   1:17.31 AliYunDun
 2423 root      20   0   14376   3648   1768 S  0.0  0.4   0:00.08 bash
 2965 root      20   0   33572   3176   2632 R  0.0  0.3   0:00.06 top
  450 root      20   0  275768   2844    100 S  0.0  0.3   0:00.37 accounts-daemon
 2078 root      20   0   14348   2188    336 S  0.0  0.2   0:00.08 bash
 1003 root      20   0   61636   2160      0 S  0.0  0.2   0:00.00 (sd-pam)
 2403 root      20   0   96032   1752    816 S  0.7  0.2   0:00.86 sshd
 2058 root      20   0   96032   1424    496 S  0.0  0.1   0:00.14 sshd
  878 root      20   0  127332   1396      0 S  0.0  0.1   0:00.00 nginx
  247 root      20   0   44740   1244      0 S  0.0  0.1   0:00.09 systemd-udevd
  854 ntp       20   0   98200   1008    432 S  0.0  0.1   0:01.35 ntpd
  999 root      20   0   36800    872    124 S  0.0  0.1   0:00.00 systemd
  463 syslog    20   0  256396    728      0 S  0.0  0.1   0:00.06 rsyslogd
  960 root      20   0   59432    684      0 S  0.0  0.1   0:00.00 sshd
  519 root      20   0   32280    636      0 S  0.0  0.1   0:07.56 AliYunDunUpdate
  402 root      20   0   29008    600    316 S  0.0  0.1   0:00.12 cron
  212 root      20   0   28356    584    264 S  0.0  0.1   0:00.12 systemd-journal
  573 root      20   0  707120    564      0 S  0.0  0.1   0:00.36 nscd
  404 root      20   0   20312    524    256 S  0.0  0.1   0:00.04 systemd-logind
  413 message+  20   0   42904    448      0 S  0.0  0.0   0:00.03 dbus-daemon
  451 root      20   0   65388    388      0 S  0.0  0.0   0:06.14 aliyun-service
  456 daemon    20   0   26044    196      0 S  0.0  0.0   0:00.00 atd
  869 root      20   0    7520     76      0 S  0.0  0.0   0:00.00 agetty
  871 root      20   0    7704     72      0 S  0.0  0.0   0:00.00 agetty
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.19 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S  0.0  0.0   0:02.09 rcu_sched
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.10 watchdog/0
   11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 perf
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
   16 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd
   17 root      39  19       0      0      0 S  0.0  0.0   0:00.70 khugepaged
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd
   20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   21 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd
   22 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ata_sff
   23 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md
   24 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 devfreq_wq
   28 root      20   0       0      0      0 S  0.7  0.0   0:48.99 kswapd0
   29 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 vmstat
   30 root      20   0       0      0      0 S  0.0  0.0   0:00.00 fsnotify_mark
   31 root      20   0       0      0      0 S  0.0  0.0   0:00.00 ecryptfs-kthrea
   47 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kthrotld
   48 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 acpi_thermal_pm
   49 root      20   0       0      0      0 S  0.0  0.0   0:00.00 vballoon
   50 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   51 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   52 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   53 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   54 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset

这是内存比较良好的情况下,一些进程的内存使用情况。

可以看到,内存占用比较高的两个进程分别是java和mysql。

java进程内存占用达到了50.7%,mysql内存占用达到了25.3。

实测下来,服务器启动后,初始化内存能占用到33%。

做了swap虚拟内存,分配了大概500M,不过实测下来,当使用到swap内存的时候,服务器cpu占用率会非常高,服务器的响应也会因此而缓慢。最好还是不要用到swap内存的为好。

之前已经调配过jvm的参数,目前峰值的时候,java进程大概能占用到55%左右。内存相对来说还是不够用的。不过jvm的参数看样子应该已经处于一个比较良好的状态了,继续调配jvm参数用来节约内存的使用看样子也起不到更好的效果。

考虑压缩mysql的内存使用。

查看mysqld的配置:

key_buffer_size         = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
max_connections         = 50
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit       = 512K
query_cache_size        = 16M

一些目前默认的参数。尝试修改了下,好像没有起到太大的作用。

在网络上爬帖,看到一些同志建议设置performance_schema = 0。尝试在mysqld配置中添加了这个配置,重启mysql服务后,内存占用如下:

top - 11:16:44 up 1 day, 57 min,  1 user,  load average: 0.34, 0.22, 0.19
Tasks:  88 total,   1 running,  87 sleeping,   0 stopped,   0 zombie
%Cpu(s): 18.3 us,  3.3 sy,  0.0 ni,  0.0 id, 78.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1016020 total,    65720 free,   769324 used,   180976 buff/cache
KiB Swap:   524284 total,   524284 free,        0 used.    92816 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 3205 root      20   0 2325020 541088      0 S  0.3 53.3  27:26.09 java
 3047 mysql     20   0 1012072 181948      0 S 19.3 17.9  33:08.67 mysqld
 1091 root      20   0  126480   3808      0 S  0.3  0.4   3:31.22 AliYunDun
  879 www-data  20   0  128016   3492   1648 S  0.0  0.3   0:32.94 nginx
 4640 root      20   0   14360   2980   1116 S  0.0  0.3   0:00.04 bash
  450 root      20   0  275768   2972    228 S  0.0  0.3   0:01.05 accounts-daemon
 4725 root      20   0   33572   2948   2408 R  0.0  0.3   0:00.00 top
    1 root      20   0   37972   2500    452 S  0.0  0.2   0:01.78 systemd
 1003 root      20   0   61636   2160      0 S  0.0  0.2   0:00.00 (sd-pam)
 4620 root      20   0   96032   1452    516 S  0.0  0.1   0:00.24 sshd
  878 root      20   0  127332   1396      0 S  0.0  0.1   0:00.00 nginx
  247 root      20   0   44740   1244      0 S  0.0  0.1   0:00.14 systemd-udevd
  999 root      20   0   36800    872    124 S  0.0  0.1   0:00.00 systemd
  463 syslog    20   0  256396    732      0 S  0.0  0.1   0:00.14 rsyslogd
  960 root      20   0   59432    684      0 S  0.0  0.1   0:00.00 sshd
  519 root      20   0   32280    636      0 S  0.0  0.1   0:21.45 AliYunDunUpdate
  854 ntp       20   0   98200    576      0 S  0.3  0.1   0:03.75 ntpd
  212 root      20   0   28356    564    244 S  0.0  0.1   0:00.27 systemd-journal
  573 root      20   0  707120    564      0 S  0.0  0.1   0:00.82 nscd
  413 message+  20   0   42904    448      0 S  0.0  0.0   0:00.03 dbus-daemon
  451 root      20   0   65388    392      0 S  0.0  0.0   0:17.87 aliyun-service
  402 root      20   0   29008    284      0 S  0.0  0.0   0:00.22 cron
  404 root      20   0   20312    268      0 S  0.0  0.0   0:00.11 systemd-logind
  456 daemon    20   0   26044    196      0 S  0.0  0.0   0:00.00 atd
  869 root      20   0    7520     76      0 S  0.0  0.0   0:00.00 agetty
  871 root      20   0    7704     72      0 S  0.0  0.0   0:00.00 agetty
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.86 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S  0.0  0.0   0:06.24 rcu_sched
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.31 watchdog/0
   11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 perf
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.02 khungtaskd
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
   16 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd
   17 root      39  19       0      0      0 S  0.0  0.0   0:01.18 khugepaged
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd
   20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   21 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd
   22 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ata_sff
   23 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md
   24 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 devfreq_wq
   28 root      20   0       0      0      0 S  0.7  0.0   1:00.58 kswapd0
   29 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 vmstat
   30 root      20   0       0      0      0 S  0.0  0.0   0:00.00 fsnotify_mark
   31 root      20   0       0      0      0 S  0.0  0.0   0:00.00 ecryptfs-kthrea
   47 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kthrotld
   48 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 acpi_thermal_pm
   49 root      20   0       0      0      0 S  0.0  0.0   0:00.00 vballoon
   50 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   51 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   52 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   53 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   54 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   55 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   56 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset

峰值情况下,mysql的内存占用大概也就只有18%左右。相比之前动辄就占用25%,目前节省了不少。看样子目前的问题大概是解决了,服务器尚有一些剩余内存用来执行其他一些耗内存的操作。

查了一下performance_schema这个参数,大致了解到这个配置参数主要用于收集数据库服务器性能。当然了,看样子目前是用不上这个参数啦~

点赞(2)

上一个文章:mysql5.7 ibtmp1文件过大

下一个文章:mysql5.7 ibtmp1文件过大

点了个评