转自 http://topcat.iteye.com/blog/1293650

问题


lucene使用排序时会将被排序字段全部加入内存再进行排序,当多次使用不同字段进行排序时会造成OOM问题

解决方案


修改lucene源码在每次查询完成后将排序所使用的FieldCache里的缓存清空

分别需要修改两个工程
  1. lucene2.4
  2. solr1.3

Lucene2.4中要修改的类或接口有


  • org.apache.lucene.index.IndexReader
添加成员变量
public String m_cacheKey = "cmhk_oom_bugfix";
  • org.apache.lucene.search.Searcher
添加抽象方法

abstract public void release();
在它的实现类中除org.apache.lucene.search.IndexSearcher外的都加上一个这个抽象方法的空实现
  • org.apache.lucene.search.IndexSearcher
添加方法
public void release() {
// 清除缓存数据
if (null !=
reader.m_cacheKey && !"".equals(reader.m_cacheKey))
{
if (null != FieldSortedHitQueue.Comparators)
{
FieldSortedHitQueue.Comparators.ReleaseCache(reader.m_cacheKey);
}
FieldCache.DEFAULT.ReleaseFieldCache(reader.m_cacheKey);
ExtendedFieldCache.EXT_DEFAULT.ReleaseExtendedFieldCache(reader.m_cacheKey);
// 立即回收垃圾
//
System.gc();
}
}
  • org.apache.lucene.search.FieldCache
添加方法
void ReleaseFieldCache(String
strCacheKey);
  • org.apache.lucene.search.FieldCacheImpl
添加方法
public void
ReleaseFieldCache(String strCacheKey)
{
bytesCache.ReleaseCache(strCacheKey);
shortsCache.ReleaseCache(strCacheKey);
intsCache.ReleaseCache(strCacheKey);
floatsCache.ReleaseCache(strCacheKey);
stringsCache.ReleaseCache(strCacheKey);
stringsIndexCache.ReleaseCache(strCacheKey);
autoCache.ReleaseCache(strCacheKey);
customCache.ReleaseCache(strCacheKey);
}

  • org.apache.lucene.search.FieldCacheImpl.Cache
添加方法
// 用于清除缓存
public
void ReleaseCache(String strCacheKey)
{
if (null != readerCache) {
synchronized (readerCache) {
if (null !=
readerCache.get(strCacheKey)) {
((Map)
readerCache.get(strCacheKey)).clear();
}
}
}
}
修改org.apache.lucene.search.FieldCacheImpl.Cache.get(IndexReader,
Object)方法
public Object get(IndexReader reader, Object key)
throws IOException {
Map
innerCache;
Object value;
synchronized (readerCache) {
//改键值为静态值
innerCache = (Map)readerCache.get(reader.m_cacheKey);
// innerCache = (Map)
readerCache.get(reader);
if
(innerCache == null) {
innerCache = new
HashMap();
//改键值为静态值
readerCache.put(reader.m_cacheKey,innerCache);
//readerCache.put(reader, innerCache);
value
= null;
} else {
value = innerCache.get(key);
}
if (value == null)
{
value = new
CreationPlaceholder();
innerCache.put(key,
value);
}
}
if
(value instanceof CreationPlaceholder)
{
synchronized (value)
{
CreationPlaceholder progress =
(CreationPlaceholder) value;
if
(progress.value == null)
{
progress.value = createValue(reader,
key);
synchronized
(readerCache) {
innerCache.put(key,
progress.value);
}
}
return
progress.value;
}
}
return
value;
}
}
  • org.apache.lucene.search.ExtendedFieldCache
添加方法
//用于清除缓存
void
ReleaseExtendedFieldCache(String
strCacheKey);
  • org.apache.lucene.search.ExtendedFieldCacheImpl
添加方法
// 用于清除缓存
public
void ReleaseExtendedFieldCache(String
strCacheKey)
{
longsCache.ReleaseCache(strCacheKey);
doublesCache.ReleaseCache(strCacheKey);
autoCache.ReleaseCache(strCacheKey);
}

solr1.3中要修改的类或接口


  • org.apache.solr.search.SolrIndexSearcher
添加方法
public void
release(){
searcher.release();
}
  • org.apache.solr.core.SolrCore
修改私有方法
private RefCounted<SolrIndexSearcher>
newHolder(SolrIndexSearcher newSearcher) {
RefCounted<SolrIndexSearcher> holder
= new
RefCounted<SolrIndexSearcher>(newSearcher) {
public void
close() {
try {
synchronized(searcherLock) {
// it's possible for someone to get a reference via the _searchers
queue
// and increment the refcount
while RefCounted.close() is being called.
// we check the refcount again to see if this has happened and
abort the close.
// This relies on the
RefCounted class allowing close() to be called every
// time the counter hits
zero.
resource.release();//释放FieldCache中的内存
if (refcount.get() >
0) return;
_searchers.remove(this);
}
resource.close();
}
catch (IOException e)
{
log.severe("Error closing searcher:"
+
SolrException.toStr(e));
}
}
};
holder.incref();
// set ref count to 1 to account for
this._searcher
return
holder;
}
  • org.apache.solr.util.RefCounted<Type>

修改方法org.apache.solr.util.RefCounted.decref()
public void
decref() {
// if (refcount.decrementAndGet() == 0)
{
close();
//
}
}

(转)solr排序OOM解决方法的更多相关文章

  1. php数组怎么根据其中的一个值进行排序?解决方法

    有一个数组如下 我想对其进行一个排序,按照其“sort”的值的大小进行排序,3->4->7. 解决方法如下: 1.首先介绍php的两个方法 方法一:array_column() 返回输入数 ...

  2. oracle数据库对date字段类型存在空值进行排序的处理方法

    oracle数据库对date字段类型存在空值进行排序的处理方法      oracle 数据库,如果表中有一个字段类型为date,且该字段类型存在空值,并且需要排序,     解决方法为使用oracl ...

  3. Spark java.lang.outofmemoryerror gc overhead limit exceeded 与 spark OOM:java heap space 解决方法

    引用自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246 ...

  4. 无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_90_CI_AI" 之间的排序规则冲突。的解决方法

    在SQL SERVICE的查询的时候遇到了“无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_90_CI_AI&q ...

  5. Java中关于OOM的场景及解决方法

    原文地址:http://developer.51cto.com/art/201112/305696.htm 1.OOM for Heap=>例如:java.lang.OutOfMemoryErr ...

  6. Java中关于OOM的场景及解决方法(转)

    http://developer.51cto.com/art/201112/305696.htm 1.OOM for Heap=>例如:java.lang.OutOfMemoryError: J ...

  7. layui表格点击排序按钮后,表格绑定事件失效解决方法

    最近项目使用layui较为频繁,遇到了一个麻烦的问题,网上搜索也没有看到同类型的问题,故此记下来. 需求是点击上图右侧表格中某一个单元格,会触发点击事件如下代码: $("table>t ...

  8. Spark面对OOM问题的解决方法及优化总结 (转载)

    转载地址: http://blog.csdn.net/yhb315279058/article/details/51035631     Spark中的OOM问题不外乎以下两种情况 map执行中内存溢 ...

  9. [转载] java中关于OOM的场景及解决方法

    1.OOM for Heap=>例如:java.lang.OutOfMemoryError: Java heapspace[分析] 此OOM是由于JVM中heap的最大值不满足需要,将设置hea ...

随机推荐

  1. oracle 多条执行语句同时执行

    oracle 多条执行语句同时执行:begin 语名一;语句二; end; 注意 如果写在C#里千万不要用@的方法然后语句里有回车 例: string strSql = "begin upd ...

  2. P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

    裸的强连通 ; type node=record f,t:longint; end; var n,m,dgr,i,u,v,num,ans:longint; bfsdgr,low,head,f:arra ...

  3. Unity3d 协程、调用函数、委托

    (一)协程 开启方法:StartCoroutine("函数名"): 结束方法StopCoroutine("函数名"),StopAllCoroutines(); ...

  4. javascript高级编程笔记06(面相对象2)

    1)  构造函数模式 es中的构造函数可以用来创建特定类型的对象,像Object和Array这样的原生构造函数,在运行时会自动出现在执行环境中,此外,也可以创建自定义的构造函数,从而定义自定义对象类型 ...

  5. Java中的break与continue区别

    break跳出当前循环执行循环下面的程序, 如果break出现在嵌套循环的内层循环, 则break语句只会跳出当前层的循环; 当程序执行到continue时时, 则跳过本次循环程序重新回到循环开始继续 ...

  6. unity脚本的基础语法

    基本的回调方法 Strat()方法:在游戏场景加载时被调用,在该方法内可以写一些游戏场景初始化之类的代码. update():在每一帧渲染之前被调用,大部分游戏代码在这里执行,除了物理部分的代码. F ...

  7. Linux与Windows中动态链接库的分析与对比

    摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Linux系统中都有动态库的概念,采用动态库可以有效的减少程序大小,节省空间,提高效率,增加程序的可扩展性,便于模块化管理.但不同操作系 ...

  8. ExtJS4.2学习(10)分组表格控件--GroupingGrid(转)

    鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-17/179.html ------------- ...

  9. 转Spring+Hibernate+EHcache配置(二)

    Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系 ...

  10. grep正则表达式后面的单引号和双引号的区别

    单引号''是全引用,被单引号括起的内容不管是常量还是变量者不会发生替换:双引号""是部分引用,被双引号括起的内容常量还是常量,变量则会发生替换,替换成变量内容! 一般常量用单引号' ...