linux redis 内存,Linux运维知识之设置Redis最大占用内存

本文主要向大家介绍了Linux运维知识之设置Redis最大占用内存,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

b24700f52dff0dfdcac70446098720a4.png

设置Redis最大占用内存

Redis需要设置最大占用内存吗?如果Redis内存使用超出了设置的最大值会怎样?

设置Redis最大占用内存

Redis设置最大占用内存,打开redis配置文件,找到如下段落,设置maxmemory参数,maxmemory是bytes字节类型,注意转换。修改如下所示:

# In short... if you have slaves attached it is suggested that you set a lower# limit for maxmemory so that there is some free RAM on the system for slave# output buffers (but this is not needed if the policy is 'noeviction').## maxmemory maxmemory 268435456

本机服务器redis配置文件路径:/etc/redis/6379.conf,由于本机自带内存只有1G,一般推荐Redis设置内存为最大物理内存的四分之三,所以设置0.75G,换成byte是751619276.

可以在CentOS下输入命令:find / -name redis查找redis目录:

[root@iZ94r80gdghZ ~]# find / -name redis/usr/share/nginx/html/blog.tanteng.me/wp-content/cache/supercache/blog.tanteng.me/tag/redis/etc/redis/var/redis

Redis配置文件一般在etc下的redis安装目录下。

Redis使用超过设置的最大值

如果Redis的使用超过了设置的最大值会怎样?我们来改一改上面的配置,故意把最大值设为1个byte试试。

# output buffers (but this is not needed if the policy is 'noeviction').## maxmemory maxmemory 1

打开debug模式下的页面,提示错误:OOM command not allowed when used memory > ‘maxmemory’.

设置了maxmemory的选项,redis内存使用达到上限。可以通过设置LRU算法来删除部分key,释放空间。默认是按照过期时间的,如果set时候没有加上过期时间就会导致数据写满maxmemory。

如果不设置maxmemory或者设置为0,64位系统不限制内存,32位系统最多使用3GB内存。

LRU是Least Recently Used 近期最少使用算法。

volatile-lru -> 根据LRU算法生成的过期时间来删除。

allkeys-lru -> 根据LRU算法删除任何key。

volatile-random -> 根据过期设置来随机删除key。

allkeys->random -> 无差别随机删。

volatile-ttl -> 根据最近过期时间来删除(辅以TTL)

noeviction -> 谁也不删,直接在写操作时返回错误。

如果设置了maxmemory,一般都要设置过期策略。打开Redis的配置文件有如下描述,Redis有六种过期策略:

# volatile-lru -> remove the key with an expire set using an LRU algorithm# allkeys-lru -> remove any key accordingly to the LRU algorithm# volatile-random -> remove a random key with an expire set# allkeys-random -> remove a random key, any key# volatile-ttl -> remove the key with the nearest expire time (minor TTL)# noeviction -> don't expire at all, just return an error on write operations

那么打开配置文件,添加如下一行,使用volatile-lru的过期策略:

maxmemory-policy volatile-lru

保存文件退出,重启redis服务。

info命令查看Redis内存使用情况

如服务器Redis所在目录:/usr/local/redis-3.0.7/src

在终端输入./redis-cli,打开Redis客户端,输入info命令。

出来如下信息:

[root@iZ94r80gdghZ src]# ./redis-cli127.0.0.1:6379> info# Serverredis_version:3.0.7redis_git_sha1:00000000redis_git_dirty:0redis_build_id:f07a42660a61a05eredis_mode:standaloneos:Linux 3.10.0-327.10.1.el7.x86_64 x86_64arch_bits:64multiplexing_api:epollgcc_version:4.8.5process_id:2165run_id:8ec8a8dc969d6e2f2867d9188ccb90850bfc9acbtcp_port:6379uptime_in_seconds:668uptime_in_days:0hz:10lru_clock:15882419config_file:/etc/redis/6379.conf# Clientsconnected_clients:1client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0# Memoryused_memory:816232used_memory_human:797.10Kused_memory_rss:7655424used_memory_peak:816232used_memory_peak_human:797.10Kused_memory_lua:36864mem_fragmentation_ratio:9.38mem_allocator:jemalloc-3.6.0# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1458722327rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:-1rdb_current_bgsave_time_sec:-1aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:ok# Statstotal_connections_received:1total_commands_processed:0instantaneous_ops_per_sec:0total_net_input_bytes:14total_net_output_bytes:0instantaneous_input_kbps:0.00instantaneous_output_kbps:0.00rejected_connections:0sync_full:0sync_partial_ok:0sync_partial_err:0expired_keys:0evicted_keys:0keyspace_hits:0keyspace_misses:0pubsub_channels:0pubsub_patterns:0latest_fork_usec:0migrate_cached_sockets:0# Replicationrole:masterconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0# CPUused_cpu_sys:0.30used_cpu_user:0.29used_cpu_sys_children:0.00used_cpu_user_children:0.00# Clustercluster_enabled:0# Keyspacedb0:keys=1,expires=1,avg_ttl=425280

其中used_memory:816232,仅用了0.7M左右。

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值