我有一个Java类,由于RAM要求,需要在单独的机器上运行。该类需要~5GB或RAM。因此,我开发了一种使用套接字的解决方案,以便允许机器进行通信。 为简单起见,我们假设套接字服务器包装器下的繁重类是服务类。连接到套接字服务器的客户端类假设是客户端类。
当Socket Server在本地运行时,一切都很完美。服务器作为独立应用程序运行,每次开始实验时都无需重新加载~5GB。但是,我意识到当服务类在LAN上的另一台机器上运行时,性能非常慢。问题不在于传输速率,因为在监控时我看到~50K / s的最大传输速率。
在连接到服务器的客户端类上,使用BufferedInput
/ Output
流来使用缓冲。
让我提供服务器主题:
public void run() {
try
{
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ServerTuple tuple;
double i=0;
while ((tuple = (ServerTuple) ois.readObject()) != null) {
double[] vector;
String action=tuple.action;
String content=tuple.content;
if(action.equals("wordVector"))
{
vector=Word2Vec.getWordVector(tuple.content);
}
else if(action.equals("avgVector2"))
{
content=content.replaceAll("\\s+", " ");
vector=Word2Vec.getWordVector2(content.split(" "));
}
else
vector=null;
tuple.setVector(vector);
oos.writeObject(tuple);
oos.flush();
}
//oos.close();
}
catch(Exception e){
return;
}
}
客户程序:
public static void init(String host,int port) throws UnknownHostException, IOException {
socket=new Socket(host,port);
ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
public static ServerTuple sendData(ServerTuple tuple) throws UnknownHostException, IOException
{
try
{
if(socket==null)
{
init();
}
oos.writeObject(tuple);
oos.flush();
ServerTuple returnTuple;
while ((returnTuple = (ServerTuple) ois.readObject()) != null) {
if(returnTuple.vector==null)
return null;
return returnTuple;
}
return null;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}