- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用连接到 Wi-Fi 外围设备。我正在使用 Socket.getInputStream()
和 Socket.getOutputStream()
来读取/写入数据。建立连接后,我会存储这两个流,以便只要连接上就可以重用它们。我的应用程序每秒通过 OutputStream
发送一个命令,并通过其 read()
方法从 InputStream
读取结果。一段时间后,我得到一个“OutOfMemoryError”。如果我错了请纠正我,但我认为这是因为 read()
不会从 InputStream
中删除读取的数据,对吧?
我的问题是:存储流是一种好的做法吗?还是应该在每次发送新命令时都使用 Socket.getInputStream()
、Socket.getOutputStream()
?
这似乎不是 OutputStream
的问题,因为我可以为此调用 flush()
。InputStream
的reset()
怎么样?这会删除流的数据吗?
这是我封装流的代码:
@Override
public InputStream getInputStream() throws IOException {
return _Socket.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
return _Socket.getOutputStream();
}
@Override
public void connect() throws IOException {
try {
SocketAddress socketAddress = new InetSocketAddress(_ip, _port);
_Socket = new Socket(_ip, _port);
} catch (IOException e) {
MyExceptionHandler.appendLog(MyExceptionHandler.exceptionToString(e));
throw e;
}
}
发送和接收命令的代码来自这个api:
异常也不会立即出现。它发生在 ~30 分钟后,发送/接收了大量命令
最佳答案
Correct me if I’m wrong but I think this is because “read()” does not remove the read data from the InputStream, right?
错了。如果内存不足,那不是因为 InputStream。您的代码中有错误。
My question is: Is it a good practice to store the Streams?
是的。
Or should I use “Socket.getInputStream(), Socket.getOutputStream() every time I send a new command?
没有。
What about “reset()” of InputStream? Removes this the data for the stream?
不,它按照 Javadoc 中的说明进行操作。
编辑 您链接到的代码起初是一堆垃圾。例如,它从不检查流的结尾,所以当发生这种情况时,它将永远读取,累积 0xff 字节并最终填满内存。找到更好的东西。
关于java - 从 InputStream 读取导致 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029333/
我正在尝试将用户提供的经纬度值与数据库中的经纬度值进行比较。如果它们在彼此半径 15 公里内,则应更改 TextView 。但我面临以下错误, 我的数据库包含值 source lat = 19.218
我在我的应用程序中使用改造来下载一些媒体文件,如视频、mp3、jpg、pdf 等。当我想下载一个 55MB 的 mp4 格式的大文件时,这是一个问题。当我想下载这个文件时,我收到这样的错误: OutO
所以我正在创建一个 Android 应用程序,这段代码引发了 "Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while
直到昨天,我的应用程序运行良好,但我所做的是,由于某些原因,我不得不在 Android Studio 中打开具有不同工作空间的同一个应用程序。从那时起,当我尝试运行该应用程序时,我遇到了以下异常,所以
我正在尝试构建一个应用程序,其中客户端将其屏幕发送到服务器,客户端仅在上次发送屏幕和最新捕获的屏幕之间存在差异时才发送其屏幕(以便该程序在网络)。服务器使用 JFrame 和 JLabel 来显示图像
我正在尝试使用内存映射模式在 cupy 中加载一些较大的 .npy 文件,但我不断遇到 OutOfMemoryError 。 我认为,由于它是在内存映射模式下打开的,因此此操作不应该占用太多内存,因为
我正在尝试对基于 ant 的(Netbeans RCP)项目进行分级并找到奇怪的分级行为。 我用探查器做了一些观察,得到了下一个结果。 环境配置 Gradle 1.9 Build time: 20
我有一个应用程序可以进行网络调用并检索 XML 数据。如果没有太多数据,下面的代码可以正常工作。 public class WebClient { private static final S
在我的应用程序中,我每 3 分钟刷新一次数据。如果应用程序可以工作几个小时,我会遇到这样的错误: java.lang.OutOfMemoryError at org.apache.http.util.
我在我的一个应用程序中偶尔收到 OutOfMemoryError: (Heap Size=49187KB, Allocated=41957KB)。我该怎么做才能诊断? 01-09 10:32:02
对于学校项目,我必须编写不同类型的算法。问题是,我得到了一个工作算法。但是我必须多次运行它,一段时间后它给了我以下错误: Exception in thread "main" java.lang.Ou
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Recursive function causing a stack overflow 完成示例惰性序列 here
我收到 java.lang.OutOfMemoryError 错误,即使我还有足够的空闲 RAM。我进行的内存转储在 200MB 到 1GB 之间,而我的服务器有 24GB 的 RAM。我设置了 -X
我不明白为什么这段代码没有OutOfMemoryError public static void main(String[] args) { Object[] ref = new Object
我正在使用这个语句 //some code int a[][]=new int[5000000][5000000]; //some code 并使用命令运行它 java -mx512m Test 它给
今天我在玩OOM错误,我发现了一些我自己无法解释的东西。 我尝试分配一个比堆大的数组,期望 “请求的阵列大小超出 VM 限制”错误,但我得到一个“ Java 堆空间 ”错误。 根据JDK 11 doc
我有一个显示图像的简单页面。来源是 URL var img = new Image (); var source = new UriImageSource { Uri =
我有一个 Java Spring Boot 应用程序。它是一个非常大的应用程序,具有许多服务,并且可以执行大量任务。我尝试实现的新任务之一是从 Oracle DB 读取一些数据并通过 REST 将其发
我正在尝试使用流读取一个非常大的文件,因此我需要并行流而不是每行迭代...我正在尝试如下: String cont = new String(Files.readAllBytes(Paths.get(
假设我们的最大内存为 256M,为什么这段代码可以工作: public static void main(String... args) { for (int i = 0; i < 2; i++)
我是一名优秀的程序员,十分优秀!