• 小内存VPS优化笔记


    1.查看系统CPU使用

    [root@web ~]# uptime
     23:13:13 up 4 min,  2 users,  load average: 0.00, 0.03, 0.00
    

    发现使用率不是太高,排除程序占用CPU。

    2.查看内存占用

    [root@web ~]# free -m
                 total       used       free     shared    buffers     cached
    Mem:           384        331         52          0          0          0
    -/+ buffers/cache:        331         52
    Swap:            0          0          0

    我的VPS是Guaranteed RAM 192MB,Burstable RAM 384MB。目前来看内存已经消耗殆尽了。

    PS:写这段话期间VPS已经无法分配内存了,由于是采用VPS 基于OpenVZ,且没有虚拟内存,所以基本上内存不足的时候VPS不会崩溃,已经运行的程序可以接着运行,只不过是不是的内存分配不足罢了。

    [root@web ~]# top
    -bash: fork: Cannot allocate memory

    3.查看各进程的内存占用,就是上面那条,重启VPS之后接着来

    top - 23:25:33 up 4 min,  1 user,  load average: 0.00, 0.00, 0.00
    Tasks:  24 total,   1 running,  23 sleeping,   0 stopped,   0 zombie
    Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    Mem:    393216k total,   348112k used,    45104k free,        0k buffers
    Swap:        0k total,        0k used,        0k free,        0k cached
    
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
        1 root      15   0 10364  740  620 S  0.0  0.2   0:00.19 init
     1122 root      19  -4 12632  684  360 S  0.0  0.2   0:00.00 udevd
     1381 root      15   0  5924  628  504 S  0.0  0.2   0:00.00 syslogd
     1397 root      18   0 62644 1224  656 S  0.0  0.3   0:00.00 sshd
     1406 root      22   0 21660  924  708 S  0.0  0.2   0:00.00 xinetd
     1443 root      18   0 12076 1492 1192 S  0.0  0.4   0:00.03 mysqld_safe
     1976 mysql     15   0  316m  35m 6296 S  0.0  9.2   0:00.13 mysqld
     2024 root      15   0 62836 2068  680 S  0.0  0.5   0:00.00 sendmail
     2032 smmsp     21   0 58344 1776  628 S  0.0  0.5   0:00.00 sendmail
     2041 root      18   0  248m 6080 1124 S  0.0  1.5   0:00.00 php-fpm
     2042 nginx     15   0  262m  38m  18m S  0.0 10.1   0:00.62 php-fpm
     2043 nginx     15   0  250m  25m  17m S  0.0  6.5   0:00.15 php-fpm
     2044 nginx     16   0  250m  25m  17m S  0.0  6.5   0:00.13 php-fpm
     2046 nginx     15   0  250m  25m  17m S  0.0  6.5   0:00.13 php-fpm
     2047 nginx     15   0  250m  25m  17m S  0.0  6.5   0:00.14 php-fpm
     3078 root      17   0 43324 1088  300 S  0.0  0.3   0:00.00 nginx
     3079 nginx     15   0 43792 1976  788 S  0.0  0.5   0:00.00 nginx
     3080 nginx     18   0 43792 1668  480 S  0.0  0.4   0:00.00 nginx
     3088 root      18   0 20884 1164  584 S  0.0  0.3   0:00.00 crond
     3096 root      18   0 46756  824  424 S  0.0  0.2   0:00.00 saslauthd
     3097 root      18   0 46756  556  156 S  0.0  0.1   0:00.00 saslauthd
     3103 root      18   0 85936 3340 2620 S  0.0  0.8   0:00.02 sshd
     3107 root      18   0 12080 1724 1300 S  0.0  0.4   0:00.01 bash
     3135 root      18   0 12624 1196  936 R  0.0  0.3   0:00.00 top

    输入M按内存使用排序

    top - 23:26:47 up 5 min,  1 user,  load average: 0.06, 0.03, 0.00
    Tasks:  24 total,   1 running,  23 sleeping,   0 stopped,   0 zombie
    Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    Mem:    393216k total,   349340k used,    43876k free,        0k buffers
    Swap:        0k total,        0k used,        0k free,        0k cached
    
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     2042 nginx     15   0  262m  38m  18m S  0.0 10.1   0:00.62 php-fpm
     1976 mysql     15   0  316m  35m 6296 S  0.0  9.2   0:00.14 mysqld
     2043 nginx     15   0  250m  25m  17m S  0.0  6.6   0:00.32 php-fpm
     2044 nginx     15   0  250m  25m  17m S  0.0  6.6   0:00.26 php-fpm
     2046 nginx     15   0  250m  25m  17m S  0.0  6.5   0:00.13 php-fpm
     2047 nginx     15   0  250m  25m  17m S  0.0  6.5   0:00.14 php-fpm
     2041 root      18   0  248m 6080 1124 S  0.0  1.5   0:00.00 php-fpm
     3103 root      18   0 86080 3352 2620 S  0.0  0.9   0:00.03 sshd
     2024 root      18   0 62836 2068  680 S  0.0  0.5   0:00.00 sendmail
     3079 nginx     15   0 43792 1976  788 S  0.0  0.5   0:00.00 nginx
     2032 smmsp     21   0 58344 1776  628 S  0.0  0.5   0:00.00 sendmail
     3107 root      18   0 12080 1724 1300 S  0.0  0.4   0:00.01 bash
     3080 nginx     18   0 43792 1668  480 S  0.0  0.4   0:00.00 nginx
     1443 root      18   0 12076 1492 1192 S  0.0  0.4   0:00.03 mysqld_safe
     1397 root      18   0 62644 1224  656 S  0.0  0.3   0:00.00 sshd
     3135 root      15   0 12624 1200  940 R  0.0  0.3   0:00.02 top
     3088 root      18   0 20884 1164  584 S  0.0  0.3   0:00.00 crond
     3078 root      17   0 43324 1088  300 S  0.0  0.3   0:00.00 nginx
     1406 root      22   0 21660  924  708 S  0.0  0.2   0:00.00 xinetd
     3096 root      18   0 46756  824  424 S  0.0  0.2   0:00.00 saslauthd
        1 root      15   0 10364  740  620 S  0.0  0.2   0:00.19 init
     1122 root      19  -4 12632  684  360 S  0.0  0.2   0:00.00 udevd
     1381 root      15   0  5924  628  504 S  0.0  0.2   0:00.00 syslogd
     3097 root      18   0 46756  556  156 S  0.0  0.1   0:00.00 saslauthd

    注:PID进程ID,USER用户,PR优先级,NI(NICE)值越大实时性越高,VIRT虚拟内存,RES物理内存,SHR共享内存

    看出是php-fpm与mysqld2位大仙在做怪。

    找出php-fpm的配置,发现有几个值设的有点高,我打算降低一下看看结果。后来发现改善不大。

    从Mysql找突破口,http://blog.s135.com/post/375/ 这里有一篇极具参考价值的文章,so按照其建议关闭InnoDB引擎,果然大有提升。

    PS:关闭InnoDB引擎前应该把之前使用了该引擎的数据库转换为MYISAM,可以使用如下SQL语句:

    ALTER TABLE `tablename` ENGINE = MYISAM

    附上我的Mysql配置

    [mysqld]
    
    ## General
    datadir                         = /var/lib/mysql
    tmpdir                          = /var/lib/mysqltmp
    socket                          = /var/lib/mysql/mysql.sock
    skip-name-resolve
    sql-mode                        = NO_ENGINE_SUBSTITUTION
    #event-scheduler                = 1
    
    ## Cache
    thread-cache-size               = 16
    table-open-cache                = 4096
    table-definition-cache          = 2048
    query-cache-size                = 0M
    query-cache-limit               = 2M
    
    ## Per-thread Buffers
    sort-buffer-size                = 128K
    read-buffer-size                = 1M
    read-rnd-buffer-size            = 2M
    join-buffer-size                = 128K
    
    ## Temp Tables
    tmp-table-size                  = 512k
    max-heap-table-size             = 32M
    
    ## Networking
    back-log                        = 100
    #max-connections                = 200
    max-connect-errors              = 10000
    max-allowed-packet              = 8M
    interactive-timeout             = 3600
    wait-timeout                    = 600
    
    ### Storage Engines
    default-storage-engine         = MyISAM
    #innodb                          = FORCE
    skip-innodb
    
    ## MyISAM
    key-buffer-size                 = 4M
    myisam-sort-buffer-size         = 4M
    
    # InnoDB
    innodb-buffer-pool-size        = 8M
    innodb-log-file-size           = 50M
    innodb-log-buffer-size         = 8M
    innodb-file-per-table          = 1
    innodb-open-files              = 300
    
    ## Replication
    server-id                       = 1
    #log-bin                        = /var/log/mysql/bin-log
    #relay-log                      = /var/log/mysql/relay-log
    relay-log-space-limit           = 16G
    expire-logs-days                = 7
    #read-only                      = 1
    #sync-binlog                    = 1
    #log-slave-updates              = 1
    #binlog-format                  = STATEMENT
    #auto-increment-offset          = 1
    #auto-increment-increment       = 2
    
    ## Logging
    #log-output                      = FILE
    #slow-query-log                  = 1
    #slow-query-log-file             = /var/log/mysql/slow-log
    #log-slow-slave-statements
    long-query-time                 = 1
    
    [mysqld_safe]
    log-error                       = /var/log/mysqld.log
    open-files-limit                = 600
    
    [mysql]
    no-auto-rehash

    收工~

  • 相关阅读:
    第十周阅读内容
    第十周学习小结
    第九周阅读内容
    第九周学习小结
    第八周学习小结
    ..总结
    .总结
    总结.
    周总结
    总结
  • 原文地址:https://www.cnblogs.com/x2048/p/2728789.html
Copyright © 2020-2023  润新知