2016-11-16 56 views
1

我將很多文本插入到redis中以便逐行存儲頻率。但是,jedis/redis在執行一定操作後需要花費大量時間執行 e操作,並且程序以錯誤結束: java.lang.OutOfMemoryError。Jedis(Redis)減速

這裏是我的測試主要文件: 公共類臨時{

private ExecutorService executor; 

public temp() { 
    executor = Executors.newFixedThreadPool(5); 
} 

public static void main(String[] args) { 
    temp ob = new temp(); 

    System.out.println("starting"); 
    for(long i =0;i<10000000;i++) { 
     if (i%10000 == 0) { 
      System.out.println(i); 
     } 
     String x = Integer.toString(new Random().nextInt()); 
     ob.executor.submit(new Runner1("abra"+x)); 
     ob.executor.submit(new Runner2("delhi"+x)); 
    } 

    try { 
     if (ob.executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) { 
      System.out.println("completed"); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

} 


} 

這裏是我的兩個參賽者: 亞軍1:

public class Runner1 implements Runnable { 
//private static RedisClientUtil redisClient = null; 
private String key; 
private static Integer count = 0; 

public Runner1(String key) { 
    this.key = key; 
} 

public void run() { 
    try { 

     ArrayList<ArrayList<Object>> cmd = new ArrayList<ArrayList<Object>>(); 

     String offer_title = this.key + " this is thread1"; 
     String offer_title_words[] = offer_title.split(" "); 
     for (String word : offer_title_words) { 
      // INCR the frequency in reddis 
      cmd.add(GenerateUtils.getArrayList("incrBy", "test"+word, 1)); 
     } 

     List<Object> responses = RedisbenchmarkTest.getLocalhostJedisPool().executePipelinedAndReturnResponses(0,cmd); 
     cmd = null; 
     responses = null; 

     updateNumberOfRowsInserted(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

    private synchronized void updateNumberOfRowsInserted() { 
     //logging 
     count++; 
     if(count%10000==0) 
      System.out.println("Thread 1 : " + count); 
    } 



} 

亞軍2:

public class Runner2 implements Runnable { 
//private static RedisClientUtil redisClient = null; 
private String key; 
private static Integer count = 0; 

public Runner2(String key) { 
    this.key = key; 
} 

public void run() { 
    try { 

     ArrayList<ArrayList<Object>> cmd = new ArrayList<ArrayList<Object>>(); 

     String offer_title = this.key + " this is thread2"; 
     String offer_title_words [] = offer_title.split(" "); 
     for (String word : offer_title_words) { 
      // INCR the category_word in reddis 
      cmd.add(GenerateUtils.getArrayList("incrBy","test1"+word,1)); 
     }     RedisbenchmarkTest.getLocalhostJedisPool().executePipelinedWithoutReturningResponses(0,cmd); 
     updateNumberOfRowsInserted(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

private synchronized void updateNumberOfRowsInserted() { 
    //logging 
    count++; 
    if(count%10000==0) 
     System.out.println("Thread 2 : " + count); 
} 

} 

這是我的Redis客戶端:

public class RedisbenchmarkTest { 

    private static RedisClientUtil localhostJedisPool; 


private static final JedisPoolConfig standardJedisPoolConfig = new JedisPoolConfig() {{ 
    setMaxTotal(500); 
    setMaxIdle(20); 
    setMaxWaitMillis(500); 
    setTestOnBorrow(false); 
    setTestOnReturn(false); 
}}; 

private static final int semiLowTimeout = 500; 

static { 
    initialize(); 
} 

public static void initialize() { 
    localhostJedisPool = new RedisClientUtil(
      standardJedisPoolConfig 
      , "localhost" 
      , 6379 
      , semiLowTimeout 
    ); 
    } 



    public static RedisClientUtil getLocalhostJedisPool() { 
     if (localhostJedisPool == null) { 
      initialize(); 
    } 
    return localhostJedisPool; 
    } 
    } 

回答

0
  • 你應該redis.conf禁用bgsave如果Redis的開始bgsave通常巨大滯後暗示,直到保存完成。
  • 您應該確保redis.conf中的maxmemory足夠高。我猜你的實例正在運行。
+0

#存儲器 used_memory:23754992 used_memory_human:22.65M used_memory_rss:27152384 used_memory_rss_human:25.89M used_memory_peak:23754992 used_memory_peak_human:22.65M total_system_memory:8589934592 total_system_memory_human:8.00克 used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:1.14 mem_allocator:libc的 – ranger

+0

的根據INFO redis,內存不會被大量使用。 – ranger

+0

你可以運行「redis-cli info」,然後啓動你的java應用程序並查看是否立即處理命令。也許jedis正在批量執行命令並導致暫停? – edlerd

0

Redis不是內存不足的那個,你的Java過程是。

您沒有發佈完整的實際錯誤,我不是一個Java的人,但Caused by: java.lang.OutOfMemoryError: Java heap space可能會爲您提供一些答案。

+0

這可能是因爲Jedis執行得非常緩慢,最終java不見了。 – ranger