gpt4 book ai didi

java - ByteArrayInputStream 堆内存不足

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:50 28 4
gpt4 key购买 nike

我正在研究流媒体网络摄像头。
现在我可以进行 1:1 TCP 网络摄像头通信。
问题是即使 gc 运行,堆内存也不会减少。
我尝试了重置(),刷新(),关闭()。
一切看起来都很好,但 new ByteArrayInputStream(f.bytes) 却不然。
我认为图像字节堆积起来,但我不知道如何清除它。

heap memory monitor

这是我的代码。

VideoServerThread.java

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import common.Frame;

public class VideoServerThread extends Thread
{
private ServerSocket serverSocket;
int videoServerPort;
private Socket socket;
private JPanel panel;
private boolean calling;

public VideoServerThread(ServerSocket serverSocket, int videoServerPort, JPanel panel, boolean calling)
{
this.serverSocket = serverSocket;
this.videoServerPort = videoServerPort;
this.panel = panel;
this.calling = calling;
}

@Override
public void run()
{
System.out.println("Video Server opened!");
try
{
serverSocket = new ServerSocket(videoServerPort);
socket = serverSocket.accept();
InputStream in = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(in);
BufferedImage bufferedImage;
ByteArrayInputStream inputImage;
Frame f;
while (calling)
{
f = (Frame) ois.readObject();
inputImage = new ByteArrayInputStream(f.bytes);
bufferedImage = ImageIO.read(inputImage);
panel.getGraphics().drawImage(bufferedImage, 0, 0, panel.getWidth(), panel.getHeight(), null);
bufferedImage.flush();
inputImage.close();
}
}

catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}

VideoClientThread.java

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.imageio.ImageIO;

import common.Frame;
import video.VideoCap;

public class VideoClientThread extends Thread
{
private final String formatType = "jpg";
private VideoCap videoCap;
private Socket socket;
private String ip;
private int port;
private boolean calling;

public VideoClientThread(VideoCap videoCap, Socket socket, String ip, int port, boolean calling)
{
this.videoCap = videoCap;
this.socket = socket;
this.ip = ip;
this.port = port;
this.calling = calling;
}

public void run()
{
try
{
socket = new Socket(ip, port);
socket.setSoTimeout(5000);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Frame f;
BufferedImage bufferedImage;
ByteArrayOutputStream fbaos;
fbaos = new ByteArrayOutputStream();
while (calling)
{
bufferedImage = videoCap.getOneFrame();
ImageIO.write(bufferedImage, formatType, fbaos);
f = new Frame(fbaos.toByteArray());
oos.writeObject(f);
bufferedImage.flush();
fbaos.reset();
oos.reset();
}
fbaos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
  • 图像以字节数组形式存在于 Frame 对象中。

最佳答案

只需将图像直接写入套接字输出流即可。摆脱对象流和字节数组流。

关于java - ByteArrayInputStream 堆内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43386526/

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