https://blog.csdn.net/happyrabbit456/article/details/54945667

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

设置Redis最大占用内存

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

 
 
 
 
 
 

Vim

 
1
2
3
4
5
6
# 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 <bytes>
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试试。

 
 
 
 
 
 

Vim

 
1
2
3
4
# output buffers (but this is not needed if thepolicy is 'noeviction').
#
# maxmemory <bytes>
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 近期最少使用算法。

  1. volatile-lru -> 根据LRU算法生成的过期时间来删除。
  2. allkeys-lru -> 根据LRU算法删除任何key。
  3. volatile-random -> 根据过期设置来随机删除key。
  4. allkeys->random -> 无差别随机删。
  5. volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
  6. noeviction -> 谁也不删,直接在写操作时返回错误。

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

 
 
 
 
 
 

Vim

 
1
2
3
4
5
6
# 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的过期策略:

 
 
 
 
 

Vim

 
1
maxmemory-policy volatile-lru

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

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

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

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

出来如下信息:

[root@iZ94r80gdghZ src]# ./redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:f07a42660a61a05e
redis_mode:standalone
os:Linux 3.10.0-327.10.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:2165
run_id:8ec8a8dc969d6e2f2867d9188ccb90850bfc9acb
tcp_port:6379
uptime_in_seconds:668
uptime_in_days:0
hz:10
lru_clock:15882419
config_file:/etc/redis/6379.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:816232
used_memory_human:797.10K
used_memory_rss:7655424
used_memory_peak:816232
used_memory_peak_human:797.10K
used_memory_lua:36864
mem_fragmentation_ratio:9.38
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1458722327
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
total_net_input_bytes:14
total_net_output_bytes:0
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.30
used_cpu_user:0.29
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=1,avg_ttl=425280

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

转载:https://blog.tanteng.me/2016/03/redis-maxmemory/

Redis配置

Redis可以在没有配置文件的情况下通过内置的配置来启动,但是这种启动方式只适用于开发和测试。

合理的配置Redis的方式是提供一个Redis配置文件,这个文件通常叫做 redis.conf

redis.conf文件中包含了很多格式简单的指令如下:

  1.  
    keyword argument1 argument2 ... argumentN
  2.  
    关键字 参数1 参数2 ... 参数N

如下是一个配置指令的示例:

slaveof 127.0.0.1 6380

如果参数中含有空格,那么可以用双引号括起来,如下:

requirepass "hello world"

这些指令的配置,意义以及深入使用方法都能在每个Redis发布版本自带的的redis.conf文档中找到。

通过命令行传参

自Redis2.6起就可以直接通过命令行传递Redis配置参数。这种方法可以用于测试。 以下是一个例子:这个例子配置一个新运行并以6380为端口的 Redis实例,使配置它为127.0.0.1:6379Redis实例的slave。

./redis-server --port 6380 --slaveof 127.0.0.1 6379

通过命令行传递的配置参数的格式和在redis.conf中设置的配置参数的格式完全一样, 唯一不同的是需要在关键字之前加上 前缀--

需要注意的是通过命令行传递参数的过程会在内存中生成一个临时的配置文件(也许会直接追加在 命令指定的配置文件后面),这些传递的参数也会转化为跟Redis配置文件一样的形式。

运行时配置更改

Redis允许在运行的过程中,在不重启服务器的情况下更改服务器配置,同时也支持 使用特殊的CONFIG SET和 CONFIG GET命令用编程方式查询并设置配置。

并非所有的配置指令都支持这种使用方式,但是大部分是支持的。更多相关的信息请查阅CONFIG SET和 CONFIG GET页面。

需要确保的是在通过CONFIG SET命令进行的设置的同时,也需在 redis.conf文件中进行了相应的更改。 未来Redis有计划提供一个CONFIG REWRITE命令在不更改现有配置文件的同时, 根据当下的服务器配置对redis.conf文件进行重写。

配置Redis成为一个缓存

如果你想把Redis当做一个缓存来用,所有的key都有过期时间,那么你可以考虑 使用以下设置(假设最大内存使用量为2M):

  1.  
    maxmemory 2mb
  2.  
    maxmemory-policy allkeys-lru

以上设置并不需要我们的应用使用EXPIRE(或相似的命令)命令去设置每个key的过期时间,因为 只要内存使用量到达2M,Redis就会使用类LRU算法自动删除某些key。

相比使用额外内存空间存储多个键的过期时间,使用缓存设置是一种更加有效利用内存的方式。而且相比每个键固定的 过期时间,使用LRU也是一种更加推荐的方式,因为这样能使应用的热数据(更频繁使用的键) 在内存中停留时间更久。

基本上这么配置下的Redis可以当成memcached使用。

当我们把Redis当成缓存来使用的时候,如果应用程序同时也需要把Redis当成存储系统来使用,那么强烈建议 使用两个Redis实例。一个是缓存,使用上述方法进行配置,另一个是存储,根据应用的持久化需求进行配置,并且 只存储那些不需要被缓存的数据。

请注意:用户需要详细阅读示例redis.conf文件来决定使用什么内存上限处理策略。

设置Redis最大占用内存的更多相关文章

  1. Redis对象占用内存分析

    当你往Redis中插入了一系统对象,如何分析这些对象的占用情况? 1.我们可以在Redis的控制台使用info命令来查看各项指标,其中有一项是Memory,可以通过存储前后的used_memory差异 ...

  2. Redis系列之-—内存淘汰策略(笔记)

    一.Redis ---获取设置的Redis能使用的最大内存大小 []> config get maxmemory ) "maxmemory" ) " --获取当前内 ...

  3. Redis达到最大占用内存后的淘汰策略

    1. 查询Redis最大占用内存 # 查询最大占用内存 config get maxmemory # 为0时在64操作系统中不限制内存,在32位操作系统中最大为3GB 2. Redis设置最大占用内存 ...

  4. Redis需要多少内存预留-内存占用多少才安全

    转: Redis需要多少内存预留-内存占用多少才安全 2018年02月10日 18:13:37 常城 阅读数:10280   版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  5. Windows Server 2008 MetaFile设置占用内存限制

    最近遇到Windows Server 2008服务器内存持续飙升,48G内存用了99%,查看任务管理器的进程,也没发现具体哪个进程用的内存比较大? 于是,在网上找了了一个查看内存的工具RamMap,具 ...

  6. BIOS简单设置 解析“集成显卡”内存占用问题

    很多使用集成显卡的用户会发现,在系统信息窗口中,内存容量和实际不一样.比如系统内存显示4GB,可用3.48G之类.这不可用的一部分内存到哪去了? 其实减少的这部分内存是被集成显卡占用当做显存使用了.而 ...

  7. 查看redis占用内存大小的方法

    查看redis占用内存大小的方法 <pre>redis-cli auth 密码info</pre><pre># Memory used_memory:1349009 ...

  8. 如何查看redis占用内存大小

    redis缓存固然高效,可是它会占用我们系统中宝贵的内存资源,特别是当我们的项目运行了一段时间后,我们需要看一下redis占用了多少内存,那么可以用“info”命令查看. 执行info命令后,找到Me ...

  9. Linux查看redis占用内存的方法

    redis-cli auth 密码info # Memory used_memory:13490096 //数据占用了多少内存(字节) used_memory_human:12.87M //数据占用了 ...

随机推荐

  1. 2019.3.5 L261 Are All Our Organs Vital?

    Medicine has not always shown a lot of respect for the human body. Just think about the ghoulish dis ...

  2. LeetCode子集问题

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(子集当中不包括重复的元素) 代码如下: def subsets(nums): target=[[]] for num in nums ...

  3. linux执行可执行文件时报xxx:not found

    实际上是因为可执行文件执行时所依赖的动态链接库找不到,解决方法为在编译时加-static表示使用静态链接. 或者使用arm-linux-readelf -d +可执行文件,查看该可执行文件依赖的动态链 ...

  4. mac下python2.7升级到3.6

    1. 前言 Mac系统自带python2.7,本文目的是将自带的python升级到3.6版本. 网上有本多的做法是让python2.7和python3.X两个版本共存,博主并不知道,是两版本共存好,还 ...

  5. centos的mysql升级之后密码重置

    1.配置文件添加过滤密码选项 #vim  /etc/my.cnf 跳过密码校验 2.重启mysql服务 #/etc/init.d/mysqld  restart 3.#mysql -uroot -p ...

  6. selenium 自动化安装火狐谷歌插件

    谷歌插件下载地址 https://npm.taobao.org/mirrors/chromedriver selenium下载地址 https://pypi.org/simple/selenium/ ...

  7. 2019-04-08-day027-网络编程基础

    网络编程 基于同一台机器上的多个程序之间通信 可以基于文件 基于多台机器之间的通信 可以基于网络 web程序两种架构完成的: C/S :client(客户端) server(服务端) B/S :bro ...

  8. File类操作中的IOException异常

     代码: File file= newFile("demo"+File.separator+"HelloWorld.txt"); file.createNe ...

  9. Linux内核info leak漏洞

    1  Information Leak漏洞风险 从应用层软件,到hypervisor再到kernel代码,都存在Information Leak的风险.下面给出一些示例: 应用层软件:通常是应用敏感数 ...

  10. block-chain

    维护一条链,只能增加记录,不能删除.修改. 去中心化,达到共识 密码学,保证交易无法抵赖和破坏 共识机制 PoW(Proof of Work),工作量证明,是一个博弈论的应用,来防止作恶. 示例:两个 ...