jeromq
jeromq copied to clipboard
Zeromq server multi-threading problem in the case of large amount of data:java.lang.OutOfMemoryError: GC overhead limit exceeded
Zeromq server starts 20 threads, and customer service will send 6000 requests. In dealing with 1000 requests, fast, breath ran out, and in dealing with the request of the 1000 to 3000, the middle is always stop for a while, to run for a while, to the time longer and longer to stop since three thousand, to four thousand or so. Another will quote ":GC overhead limit ",but, when I put the server-side thread open only one, can run smoothly, below is the server-side code jeromq 0.5.1 jdk1.8.0_151 Windows 7
/**
* 多线程 NLP 服务器
*/
public class mtserver {
/*默认为20个NLP线程*/
static Integer WORKERNUM = 20;
private static class HeronlpWorker extends Thread {
private ZContext context;
private MyUncaughtExceptionhandler myUncaughtException;
public void setMyUncaughtException(MyUncaughtExceptionhandler myUncaughtException) {
this.myUncaughtException = myUncaughtException;
}
private HeronlpWorker(ZContext context) {
this.context = context;
}
@Override
public void run() {
Socket socket = context.createSocket(SocketType.REP);
myUncaughtException.setSkt(socket);
socket.connect("inproc://workers");
HeroNLP heroNLP = new HeroNLP();
String result = null;
String msgType = "";
while (true) {
try {
//接收命令
String command = socket.recvStr(0).trim();
System.out.println(Thread.currentThread().getName() + " Received request: [" + command + "]");
String content = null;
switch (command){
//分词
case "SEGMENT":
content = socket.recvStr(0).trim();
List<Term> terms = NLP.segment(content);
result = listToString(terms, " ");
break;
default:
while (socket.hasReceiveMore()){
socket.recvStr(0).trim();
}
result = "FAILED: 未匹配到命令!";
break;
}
} catch (Exception e) {
result = "FAILED:" + e.getMessage();
}
// 给客户端返回结果(C字符串)
System.out.println("是否有异常: " + result.contains("FAILED:"));
if(result.contains("FAILED:")){
msgType = "FAILED";
}
socket.send(msgType, 2);
socket.send(result, 0);
}
}
}
public static void main(String[] args) {
try (ZContext context = new ZContext()) {
//前端--router
Socket clients = context.createSocket(SocketType.ROUTER);
boolean bind = clients.bind("tcp://*:9002");
//后端--dealer
Socket workers = context.createSocket(SocketType.DEALER);
workers.bind("inproc://workers");
//后端启动20个服务线程
for (int thread_nbr = 0; thread_nbr < WORKERNUM; thread_nbr++) {
Thread nlpworker = new HeronlpWorker(context);
MyUncaughtExceptionhandler myUncaughtExceptionhandler = new MyUncaughtExceptionhandler();
((HeronlpWorker) nlpworker).setMyUncaughtException(myUncaughtExceptionhandler);
nlpworker.setUncaughtExceptionHandler(myUncaughtExceptionhandler);
nlpworker.start();
}
ZMQ.proxy(clients, workers, null);
}
}