我将大量文本插入redis以逐行存储频率。但是, jedis / redis在一定数量的操作和程序以错误结束后,会慢下来并花费大量时间来执行操作: java.lang.OutOfMemoryError。
这是我的主要测试文件: 公共阶级临时{
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 :(得分:0)
答案 1 :(得分:0)
Redis不是那个耗尽内存的人,你的Java流程就是。
您没有完整地发布实际错误,我不是Java人员,但tutorial可能会为您提供一些答案。