缓存类型

按照缓存的作用区域来划分,缓存一般分为:

1,线程级缓存,如hibernate的一级缓存;

2,进程级缓存也成为本地缓存,如ehcache,oscache等。在java领域这里值得说明的是,java进程为jvm的一个实例,缓存对象一般放在jvm堆中,但是利用NIO的特性也可以直接在堆外分配内存,而不受GC的影响。

经过nio的bytebuffer分配的堆外内存不受jvm垃圾回收的影响。因此可以在一定程度上避免频繁GC对性能造成的影响,同时可以获得比jvm限制的更大的内存空间(hotspot在windows的32位的系统上大概只能分配1.3G的堆内存空间)。因此现在也有一些应用会应用到堆外缓存(ehcache就集成了堆外缓存的功能)。关于堆外缓存所涉及到的主要理论和技术为java NIO的内容,NIO的内容会在其他文章中叙述,这里需要说明的是在使用堆外缓存的时候需要对对象进行序列化和反序列化(其中的原因不用再说),这在一定程度上会对缓存性能造成一定的影响。

 

下面主要对ehcache(BigMemory)中所涉及到一些堆外缓存的理论和技术进行简单介绍:


Because off-heap data is stored in bytes, there are two implications:

  • Only Serializable cache keys and values can be placed in the store, similar to DiskStore.
  • Serialization and deserialization take place on putting and getting from the store. This means that the off-heap store is slower in an absolute sense (around 10 times slower than the MemoryStore), but this theoretical difference disappears due to two effects:
    • the MemoryStore holds the hottest subset of data from the off-heap store, already in deserialized form
    • when the GC involved with larger heaps is taken into account, the off-heap store is faster on average.

除此之外还有一些注意事项,例如:


Avoiding OS Swapping

Operating systems use swap partitions for virtual memory and are free to move less frequently used pages of memory to the swap partition. This is generally not what you want when using the OffHeapStore, as the time it takes to swap a page back in when demanded will add to cache latency.

It is recommended that you minimize swap use for maximum performance.

On Linux, you can set /proc/sys/vm/swappiness to reduce the risk of memory pages being swapped out. See http://lwn.net/Articles/83588/ for details of tuning this parameter. Note that there are bugs in this which were fixed in kernel 2.6.30 and higher.

Another option is to configure HugePages. See http://unixfoo.blogspot.com/2007/10/hugepages.html

Although there's a swappiness kernel parameter that can be set to zero in Linux, it is usually not enough to avoid swapping altogether. The only surefire way to avoid any kind of swapping is either (a) disabling the swap partition, with the undesirable consequences which that may bring, or (b) using HugePages, which are always mapped to physical memory and cannot be swapped out to disk.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值