简体   繁体   English

在类中使用装饰的OutputStream / InputStream字段

[英]Using decorated OutputStream/InputStream fields in your class

This question may seem a little trivial at first, but I am experiencing odd OutOfMemory problems since I began implementing this. 这个问题起初可能看起来有点微不足道,但是自从我开始实现这个问题以来,我遇到了奇怪的OutOfMemory问题。 After looking through the Java Heap Dumps, I know the memory leak is related to the ObjectOutputStream variable. 查看Java Heap Dumps后,我知道内存泄漏与ObjectOutputStream变量有关。 Without further ado, here is the code: 不用多说,这里是代码:

In my constructor, I am setting up field variables that will hold my Input/Output Stream variables. 在我的构造函数中,我正在设置将保存输入/输出流变量的字段变量。 Along with it, I am creating two other sets of variables for when I am specifically doing IO on custom objects and then pure primitives: 与此同时,我正在创建另外两组变量,以便我在自定义对象和纯基元上专门执行IO时:

  public SingleServer(Socket s, int maxThreads) {
    client = s;
    serversCreated.incrementAndGet();
    try {
      is = client.getInputStream();
      os = client.getOutputStream();
      ois = new ObjectInputStream(is);
      oos = new ObjectOutputStream(os);
      dis = new DataInputStream(is);
      dos = new DataOutputStream(os);
    } catch (Exception e) {
      // ...
    }
    print("Client Connected");

  }

Now, before, all there was was storage of OOS, the object output stream. 现在,之前,所有存在OOS,即对象输出流。 So, you may have asking why I am going through the trouble of creating these fields? 那么,你可能会问为什么我要经历创建这些字段的麻烦? The answer is I wanted to separate the sending of primitives from the sending of custom objects vs the sending of pure bytes. 答案是我想将基元的发送与发送自定义对象与发送纯字节分开。 I thought Java was okay in separating things out like this. 我认为Java可以像这样分离出来。

What I am noticing is that suddenly and hard-to-predictingly some objects now cause OutOfMemory Error's which crash my program. 我注意到的是突然而且难以预测的一些对象现在导致OutOfMemory错误导致我的程序崩溃。 I know I can just forget about using all these different decorators for the IO classes, but I want to understand why these out of memory errors would happen? 我知道我可以忘记为IO类使用所有这些不同的装饰器,但我想了解为什么会发生这些内存不足错误?

  • Is there something fundamentally wrong with decorating your IO stream multiple times? 多次装饰IO流有什么根本性的错误吗?
  • Is it better to decorate a stream on demand, before actually using it, and then let the garbage collector handle it when its no longer needed? 在实际使用它之前按需装饰流是否更好,然后让垃圾收集器在不再需要它时处理它?
  • As an extension to the above question, before when everything was working without memory errors, the only object I actually stored was OOS. 作为上述问题的扩展,在一切工作没有内存错误之前,我实际存储的唯一对象是OOS。 Yet, now, the memory errors are related to OOS. 然而,现在,内存错误与OOS有关。 Does the creation of DOS or OS cause the garbage collector to not run properly? DOS或OS的创建是否导致垃圾收集器无法正常运行?

Wrapping the same InputStream in two independent wrappers is an unsupported idiom. 将相同的InputStream包装在两个独立的包装器中是一种不受支持的习惯用法。 The first wrapper might immediately read some input to fill its buffer; 第一个包装器可能会立即读取一些输入以填充其缓冲区; the second one may try to do the same. 第二个可能会尝试做同样的事情。 Each wrapper is free (and even encouraged) to assume that it can fully read out its underlying stream, at its own discretion. 每个包装器都是免费的(甚至鼓励),以便它可以自行决定是否可以完全读出其底层流。

So, in a nutshell, 所以,简而言之,

Is there something fundamentally wrong with decorating your IO stream multiple times? 多次装饰IO流有什么根本性的错误吗?

Yes, there is. 就在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM