使用java中的可变对象将XML数据写入OutputStream

发布于 2024-11-25 16:23:05 字数 623 浏览 3 评论 0原文

将 XML 写入 OutputStream(TCP 套接字)而不生成/创建太多不可变对象的最佳方法是什么?

所以我将数据放在 ConcurrentHashMap 中。我想循环此数据,创建自定义 XML,然后将该 XML 写入 OutputStream。这个过程会是重复的,所以我不想在转换/写入过程中生成太多的对象,这样GC就不会有太大的负载。

我一直在研究 JAXB 和 XStream,以使映射到 XML 的转换更容易,但似乎使用 JAXB 中的 XMLAdapter 和 XStream 中的转换器方法,我最终会得到在转换过程中创建的对象。

我也愿意自己动手​​。我想要一个最终重用可变对象的解决方案。

我可以使用 StringBuffer 并使用附加方法连接所有内容(XML 标签和我的数据),然后执行 mystringBuffer.toString().getBytes() 并将字节数组写入输出流。在这种方法中,我可以重用 StringBuffer 和 ByteBuffer。只有 bytearray 每次都会是一个新对象。

还有其他方法吗?

看来我有点超前了,假设内存/GC 可能有问题 - 我可能完全错误,仅仅使用 JAXB 或 XStream 可能是解决方案。然后我可以做一些性能测试来找出瓶颈。

谢谢

What is the best way to write XML to an OutputStream (TCP Socket) without generating/creating too many Immutable objects?

So I have my data in a ConcurrentHashMap. I want to loop this data, create a custom XML and then write the XML to an OutputStream. This process will be repetitive and so I dont want to generate too many objects during the conversion/writing process, so that GC doesnt have too much load.

I've been looking at JAXB and XStream, to make the Map to XML conversion easier, but it seems like with the XMLAdapter in JAXB and Convertor approach in XStream, I'll end up with objects created during the conversion process.

I'm willing to roll my own too. I want a solution where I end up reusing mutable objects.

I could use a StringBuffer and concatenate everything (XML tags and my data) using append method and then do mystringBuffer.toString().getBytes() and write the bytearray to the outputstream. In this approach I could reuse StringBuffer and ByteBuffer. Only the bytearray will be a new object each time.

Any other approach?

It seems like I'm getting ahead of my self and assuming that memory/GC could have issues - I could be totally wrong and merely using JAXB or XStream could be the solution. I could then just do some performance testing to find out bottleneck.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-12-02 16:23:05

同意斯卡夫曼的评论:

我认为你没有给予垃圾收集器足够的信任 - 它是
非常擅长处理大量短命对象。
在看到实际的对象之前,不要担心对象创建开销
性能问题。这个问题听起来很经典
过早优化。

所以是的,我还会做一些性能测试,看看是否存在您需要关注的瓶颈。

(发布这个答案主要是为了让这个问题得到一些额外的关注/有竞争力的答案,或者至少,简单地将这个问题从不断增长的未回答问题列表中删除。)

Agreed with skaffman's comment:

I don't think you're giving the garbage collector enough credit - it's
extremely good at handling a large number of short-lived objects.
Don't worry about object creation overhead until you can see an actual
problem with the performance. This question sounds like a classic case
of premature optimisation.

So yes, I'd also do some performance testing to see if there is even a bottleneck that you need to be concerned with.

(Primarily posting this answer in an attempt to either get this question some additional attention / competitive answers, or at the least, to simply remove this from the growing list of unanswered questions.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文