- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在试验 PipedInputStream
和 PipedOutputStream
并且无法理解为什么以下代码会导致 Java 堆耗尽问题。所有创建的 transient String
对象都应该被 gc 编辑。为什么我会收到 OutOfMemoryError
?
我正在尝试写入和读取 1000 个 String
对象,每个对象的长度为 100 万个字符。即使使用 -Xmx2g
调用,以下代码也会在大约一半时失败。更重要的是痕迹:
written string #453
read string #453
written string #454
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
... 表明 PipedInputStream
只是 PipedOutputStream
“背后”的一个 String
对象。我不明白为什么垃圾收集未能回收所有必要的堆内存。
import java.io.*;
import java.util.*;
class Worker implements Runnable {
private ObjectOutputStream oos;
private PipedInputStream pis;
public Worker() throws IOException {
this.pis = new PipedInputStream();
this.oos = new ObjectOutputStream(new PipedOutputStream( pis ));
}
@Override
public void run() {
try {
for (int i = 0 ; i < 1000 ; i++) {
oos.writeObject(aBigString());
System.out.printf("written string #%d\n", i);
}
oos.flush();
oos.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
private static String aBigString() {
StringBuffer sb = new StringBuffer();
for (int i = 0 ; i < 1000*1000 ; i++)
sb.append("X");
return sb.toString();
}
public PipedInputStream getInput() {
return this.pis;
}
}
public class FooMain {
public static void main(String args[]) throws IOException, ClassNotFoundException {
Worker worker = new Worker();
(new Thread(worker)).start();
ObjectInputStream ois = new ObjectInputStream(worker.getInput());
String record = null;
int i = 0;
try {
while (true) {
record = (String) ois.readObject();
System.out.printf("read string #%d", i++);
}
} catch (EOFException e) {
ois.close();
System.out.println("done.");
}
}
}
最佳答案
这与管道流无关。您遇到了对象流的经典陷阱之一。为了保留对象的身份,流将保存通过它们的所有 对象。如果您需要将这些流用于大量对象,则需要定期调用 ObjectOutputStream
上的 reset()
(但请注意,对象标识不会在重置期间保留电话)。
关于java - PipedInputStream/PipedOutputStream 紧缩在 "loop"::为什么是 "java.lang.OutOfMemoryError: Java heap space"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167595/
我正在使用java管道将数据(outstream)从解压模块(JavaUncompress类)传递到解析模块(处理类),文件很大,我想先解压文件并直接解析而不是保存解压缩的文件,然后解析。但是,它仅适
我可以为多线程使用一个 pipedoutputstream 并将其连接到一个 pipedinputstream,然后从多线程获取所有输出吗? 以下是代码片段,我想要的是修改和删除线程的输出可以用于同步
我正在使用管道输出流将 OutputStream 转换为 InputStream 因为 AWS java sdk 不允许使用 OutputStreams 我正在使用下面的代码,但是,这会间歇性地挂起。
我的目标是: 从 S3 读取文件, 更改其元数据 再次推送到S3 AWS java SDK 不允许推送输出流。因此,我必须将 outputstream 从 step2 转换为 inputstream。
我正在学习 java PipedInputStream/PipeOutputStream 。 我想读取标准输入(下面的“Source”类)并将其重定向到一个进程(此处为“grep A”),Grep 的
我正在尝试使用 PipedInputStream 和 PipedOutputStream 实现一个线程循环缓冲区,但每次当我进入 Decoder runnable 中的 mHead.write 时它都
我在 SO 上看到了两个答案,它们声称 Java 提供的 PipedInputStream 和 PipedOutputStream 类存在缺陷。但他们没有详细说明他们出了什么问题。他们真的有缺陷吗?如
当我尝试将 OutputStream 复制到 InputStream 时,我偶然发现了 PipedOutputStream 的一个非常奇怪的行为。另请参阅How to write an nt:file
我在 Java 中使用 PipedOutputStream 和 PipedInputStream。 一个线程正在生成字节并写入它们;另一个正在消耗它们。 我想确保生成字节的速度不会明显快于消耗字节的速
什么是管道流的用例?为什么不将数据读入缓冲区然后将它们写出呢? 最佳答案 BlockingQueue 或类似的集合可能会更好地为您服务,它们是线程安全的、健壮的并且扩展性更好。 关于java - Pi
我正在编写一个简单的 Swing GUI,其中包含一个用于打印调试消息和异常的文本字段。我目前在写入 PipedOutputStream 的地方设置了它,并且我有一个守护线程,它从连接的 PipedI
我有一个数据生成器,它在单独的线程中运行并将生成的数据推送到连接到 PipedInputStream 的 PipedOutputStream 中。此输入流的引用通过公共(public) API 公开,
PipedOutputStream的Android实现 write(byte[] buffer, int offset, int count) 是根据write(byte oneByte)实现的。更具
我有一个具有单读者线程模型的多写入线程。ThreadMultipleDateReceiver 类设计用于从多个线程读取。 public class ThreadMultipleDateReceiver
我在 Scala 中有以下代码: val pos = new PipedOutputStream() val pis = new PipedInputStream(pos)
我已经使用 JTextArea 开发了一个小型控制台。我阅读了一些教程并了解了一些东西。但我仍然有问题。这是我的代码。 public class Console extends JFrame {
如何正确完成管道输出端的工作?我需要写入线程终止或做一些其他工作,而读取线程读取所有写入数据直到结束。 我应该在写入端关闭管道还是什么? 更新 1 我想澄清一下......根据给定的答案,我认为设计管
我想使用 PipedOutputStream 和 PipedInputStream Java 类编写类似于Producer Consumer Problem 的示例。 注意:这是应用其原理的小示例。
我最近发现了这个成语,我想知道我是否遗漏了什么。我从未见过它使用过。我在野外使用过的几乎所有 Java 代码都倾向于将数据放入字符串或缓冲区中,而不是像这个示例(例如使用 HttpClient 和 X
我正在向 PipedOutputStream 写入一些数据,该数据已连接到 PipedInputStream,并将其传递给某些第三方 API。写入 PipedOutputStream 是在另一个线程上
我是一名优秀的程序员,十分优秀!