分析过程 使用 pprof top分析 可见 json.Marshal占第一内存. 为什么呢? 我们进一步分析
使用 tree 分析 查看到 zerolog AppendInterface 方法占用 73.32%的内存量. 而 zerolog 是一个很优秀的日志库, 比 zap 还优秀. 为什么呢?我们需要查看源码
822.70MB 73.32% | github.com/rs/zerolog/internal/json.Encoder.AppendInterface 分析源码 找到 github.com/rs/zerolog/internal/json.Encoder.AppendInterface` 366 行
// AppendInterface marshals the input interface to a string and
// appends the encoded string to the input byte slice.
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
marshaled, err := json.Marshal(i)
if err != nil {
return e.查看 Clients 属性 127.0.0.1:6379>info clients # Clients
connected_clients:1 # 已连接客户端的数量(不包括通过从属服务器连接的客户端)
client_recent_max_input_buffer:2 # 当前连接的客户端当中,最长的输出列表
client_recent_max_output_buffer:0 # 当前连接的客户端当中,最大输入缓存
blocked_clients:0 # 正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客户端的数量 查看 Memeory 属性 127.0.0.1:6379>info memory used_memory_human:1.92G # 用户数据所占用的内存,就是你缓存的数据的大小。
used_memory_rss_human:30.73M # 常驻内存, 与top占用内存一致
used_memory_peak_human:1.93G # 内存使用峰值
total_system_memory_human:1.78G # 整个系统的内存
mem_fragmentation_ratio:0.02 # 内存碎片比率. used_memory_rss/used_memory求的值. 如果小于1时,需要优化内存碎片. mem_fragmentation_ratio 查看内存碎片比率,
小于<1时,Redis实例可能会把部分数据交换到硬盘上,内存交换会严重影响Redis的性能,所以应该增加可用物理内存 大于>1时, 说明碎片占用 更多的内存, 需要整理, 在1~1.5 之间比较健康. 重启Redis服务;也能达到碎片整理目的
查看是否开启自动碎片整理: config get activedefrag
设置自动碎片整理: config set activedefrag yes
直接手动整理碎片: memory purge
redis.conf配置设置自动整理碎片
redis 4.0
# Enabled active defragmentation
# 碎片整理总开关
# activedefrag yes
# Minimum amount of fragmentation waste to start active defrag
# 当碎片达到 100mb 时,开启内存碎片整理
active-defrag-ignore-bytes 100mb
# Minimum percentage of fragmentation to start active defrag
# 当碎片超过 10% 时,开启内存碎片整理
active-defrag-threshold-lower 10
# Maximum percentage of fragmentation at which we use maximum effort
# #内存碎片超过 100%,则尽最大努力整理
active-defrag-threshold-upper 100
# Minimal effort for defrag in CPU percentage
# 内存自动整理占用资源最小百分比
active-defrag-cycle-min 25
# Maximal effort for defrag in CPU percentage
# 内存自动整理占用资源最大百分比
active-defrag-cycle-max 75 查看 Stats 属性 只列出部分属性.参考 Go 单元测试/性能测试
性能测试 go test -test.bench=. -test.benchmem 指定方法 go test -test.bench=MyFunc -test.benchmem cpu 性能分析 go test -test.bench=MyFunc -test.cpuprofile cpu.out 内存分析 go test -test.bench=MyFunc -test.memprofile mem.out goroutine 阻塞分析 go test -test.bench=MyFunc -test.blockprofile block.out ### 指定几个cpu分析
go test -test.bench=MyFunc -test.benchmem -test.cpu 1,2,4解析方案 // 替换 ioutil.ReadAll
func ReadAll(data io.ReadCloser) (body []byte) {
buffer := bytes.NewBuffer(make([]byte, 0, 65536))
io.Copy(buffer, data)
temp := buffer.Bytes()
length := len(temp)
if cap(temp) > (length + length/10) {
body = make([]byte, length)
copy(body, temp)
} else {
body = temp
}
return
} 参考 [golang]内存不断增长bytes.makeSlice
Golang Slices And The Case Of The Missing Memorytop 是 linux 最常用的命令, 包括很多少直观的信息, 有利于我们对系统运行状态的把握.
top 使用 top 系统自带命令,可以直接使用.
top top 详情 a. 如图编号(1)
top - 10:34:07 up 16 min, 1 user, load average: 0.00, 0.01, 0.05 10:34:07 当前时间 up 16 min 系统运行时间, 如 16 分钟 1 user 当前登陆用户数 load average: 0.00, 0.01, 0.05 系统负载. 三个数值分别为 1分钟、5分钟、15分钟前到现在的平均值。 b. 如图编号(2)
Tasks: 206 total, 1 running, 205 sleeping, 0 stopped, 0 zombie Tasks: 206 total 进程总数 1 running 正在运行的进程数 205 sleeping 睡眠进程数 0 stopped 停止进程数 0 zombie 僵尸进程数 c.1. MySQL 并发参数调整 1.1. max_connections 参数 该参数设置mysql连接最大数量. max_connections 默认151个连接.
show variables like 'max_connections' 服务器性能优时可以调节这个参数, 范围: 500~1000
注 当连接过大时, 查看 Connection_errors_max_connections 参数是否大于0 , 表示连接过多, 错误连接
show status like 'Connection_errors_max_connections'; 2. back_log 参数 积压栈的大小.
也就是说当 mysql 连接超过 max_connections 连接数时, 如果back_log大小为0时, mysql将授予连接资源. 如果back_log大于零时,则接受多余的请求, 以等待某一连接释放.而等待的连接数大于back_log数时则也将不授予连接资源.
back_log默认大小: 50 + (max_connections/5), 最大可设置为900
show variables like 'back_log' 3. table_open_cache 该参数用来控制所有SQL语句执行线程可打开表缓存的数量.
最大数量设定: max_connections * N
show variables like 'table_open_cache' 4. thread_cache_size 该参数可控制 mysql缓存客户服务线程的数量, 相当于mysql的线程池, 也备重用.
show variables like 'thread_cache_size' 5.MySQL 运行的状态 重点关注以下参数
show status like 'Queries';
show status like 'Threads_connected';
show status like 'Threads_running';
show status like 'Connection_errors_max_connections'; MySQL 运行线程 show processlist 开启慢查询日志 一、参数查询
slow_query_log 开启慢查询
mysql> show variables like '%slow_query_log%'; +---------------------+--------------------------------------+ | Variable_name | Value | +---------------------+--------------------------------------+ | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/7709d56792f9-slow.log | +---------------------+--------------------------------------+ 2 rows in set (0.00 sec) set global slow_query_log=1; slow_query_log_file 慢日志存放位置
set global slow_query_log_file='/data/logs/slow-mysql.log'; long_query_time 表示1秒的SQL就记录
mysql> show variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.systemd是Linux电脑操作系统之下的一套中央化系统及设置管理程序(init),包括有守护进程、程序库以及应用软件 在Unix中常以“d”作为系统守护进程(英语:daemon,亦称后台进程)的后缀标识
systemctl 命令的使用 以下以 redis 为例
启动 systemctl start redis 停止 systemctl stop redis 重启 systemctl restart redis 状态 systemctl status redis 刷新服务 systemctl daemon-reload ### 设置开机启动
systemctl enable redis 自定义配置 新建 systemctl 文件 以 redis 服务为例
touch /lib/systemd/system/redis.service
vim /lib/systemd/system/redis.service 配置文件 vim /lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/usr/local/bin/redis-server -s reload
ExecStop=/usr/local/bin/redis-server -s stop
PrivateTmp=true
Type=simple
KillMode=process
Restart=on-failure
RestartSec=3s
[Install]
WantedBy=multi-user.target [Unit] 表示这是基础信息
Description 是描述 After 是在那个服务后面启动,一般是网络服务启动后启动 [Service] 表示这里是服务信息