gpt4 book ai didi

java - 为什么我的 MINA 应用程序中已用堆总是增加?

转载 作者:行者123 更新时间:2023-12-02 07:24:43 26 4
gpt4 key购买 nike

我有一个应用程序,由服务器和客户端两部分组成。

它的工作原理如下:

客户端连接服务器并发送字符串;服务器接收字符串并返回一个ArrayList(通过字符串转换),其中包含10000个元素。

我编写了一个类 (ClientConnector.java),它模拟许多客户端使用一个连接从服务器获取这 10000 个元素。

当我运行这两个程序时,服务器端正常。然而在客户端,使用的堆总是在增加!我试图通过“null”释放使用过的对象,但是使用的内存仍然越来越大。

http://s10.postimage.org/egf4ugrd5/mem.png

我的服务器端代码:客户端.java

公共(public)类客户端{

private static final int PORT = 7571;
ClientHandler handler = new ClientHandler("hey");
IoConnector connector;
boolean available = true;

public synchronized void setAvailable(boolean available) {
this.available = available;
}

public synchronized boolean isAvailable() {
return available;
}

public void starter() throws InterruptedException {

Thread t = new Thread(new Runnable() {
@Override
public void run() {

connector = new NioSocketConnector();
connector.getSessionConfig().setReadBufferSize(2048);
TextLineCodecFactory t = new TextLineCodecFactory(Charset.forName("UTF-8"));
t.setEncoderMaxLineLength(20 * 150000);
t.setDecoderMaxLineLength(20 * 150000);
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(t));

connector.setHandler(handler);
ConnectFuture future = connector.connect(new InetSocketAddress("localhost", PORT));
future.awaitUninterruptibly();

if (!future.isConnected()) {
return;
}

IoSession session = future.getSession();
session.getConfig().setUseReadOperation(true);
session.getCloseFuture().awaitUninterruptibly();

connector.dispose();
}
});
t.start();
Thread.sleep(300);
}

public void conClose() {
connector.dispose();
}

public ClientHandler getHandler() {
return handler;
}

public void reqInf() {
handler.reqInfo();
}

public static void main(String[] args) {
try {
Client c = new Client();
c.starter();
} catch (InterruptedException ex) {
System.out.println("error");
}
}

}

ClientHandler.java

公共(public)类 ClientHandler 扩展了 IoHandlerAdapter {

long time;
private final String values;
IoSession session;

public ClientHandler(String values) {
this.values = values;
}

@Override
public void sessionOpened(IoSession session) throws InterruptedException {
this.session = session;
}

public ArrayList<String> convert(String str) {
Gson gson = new Gson();
return gson.fromJson(str, ArrayList.class);
}

@Override
public void messageReceived(IoSession session, Object message) throws InterruptedException {

try {
ArrayList<String> test = convert(message.toString());
System.out.println("TIME : " + (System.currentTimeMillis() - time) + " strList:" + test.size());
message = null;
test = null;

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

@Override
public void exceptionCaught(IoSession session, Throwable cause) {
session.close();
System.out.println(cause.toString());
}

@Override
public void sessionClosed(IoSession session) {

System.out.println("Connection Lost");
}

public void reqInfo() {
time = System.currentTimeMillis();
session.write("test");
}

}

我的服务器端:服务器.java

公共(public)类服务器{

private static final int PORT = 7571; //TEST PORT
IoAcceptor acceptor = new NioSocketAcceptor();

public Server() throws IOException {
TextLineCodecFactory t = new TextLineCodecFactory(Charset.forName("UTF-8"));
t.setEncoderMaxLineLength(20*150000);
t.setDecoderMaxLineLength(20*150000);
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(t));
// acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));

Executor executor = new ThreadPoolExecutor(5, 70, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(executor));
acceptor.setHandler(new ServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 1000);

//timer();

acceptor.bind(new InetSocketAddress(PORT));
System.out.println("***Mina Server is ready !");
System.out.println("");
System.out.println("");


}


public static void main(String[] args) throws IOException {
Server m = new Server();
}

}

ServerHandler.java

公共(public)类 ServerHandler 扩展了 IoHandlerAdapter {

private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
IoSession sessions;
//Communication communication;

public ServerHandler() throws IOException {
loader();
// communication = new Communication(this);
}

@Override
public void sessionOpened(IoSession session) {
// set idle time to 10 seconds
session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 1000);
System.out.println("Client Connected !!!");
//session.setAttribute("Values: ");
this.sessions = session;

}

public String toGSon(ArrayList<String> list) {
Gson gson = new Gson();
String str = gson.toJson(list);
return str;
}
ArrayList<String> str = new ArrayList<String>();

public void loader() {
for (int i = 0; i < 10000; i++) {
str.add("test" + i);
}
}

@Override
public void messageReceived(IoSession session, Object message) throws InterruptedException {

long time = System.currentTimeMillis();
session.write(toGSon(str));
System.out.println("TIME : " + (System.currentTimeMillis() - time));

}

@Override
public void sessionIdle(IoSession session, IdleStatus status) {
System.out.println("Socket #" + session.getId() + " is disconnecting... (IDLE)");
session.close();
}

@Override
public void exceptionCaught(IoSession session, Throwable cause) {
System.out.println("------------>" + cause.toString());
session.close();
}

}

还有我的主课

公共(public)类 ClientConnector {

public ClientConnector() throws InterruptedException {
Client cl = new Client();
cl.starter();
while (true) {

cl.reqInf();
Thread.sleep(100);
}
}

public static void main(String[] args) throws InterruptedException {
ClientConnector cl = new ClientConnector();
}

}

最佳答案

您必须从客户端删除以下代码。

session.getConfig().setUseReadOperation(true);

以上代码会导致内存泄漏。

关于java - 为什么我的 MINA 应用程序中已用堆总是增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13699010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com