简体   繁体   English

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

[英]Writing XML Data to OutputStream with Mutable objects in java

What is the best way to write XML to an OutputStream (TCP Socket) without generating/creating too many Immutable objects?在不生成/创建太多不可变对象的情况下将 XML 写入 OutputStream(TCP 套接字)的最佳方法是什么?

So I have my data in a ConcurrentHashMap.所以我在 ConcurrentHashMap 中有我的数据。 I want to loop this data, create a custom XML and then write the XML to an OutputStream.我想循环这些数据,创建一个自定义 XML,然后将 XML 写入 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.这个过程将是重复的,所以我不想在转换/写入过程中生成太多的对象,这样 GC 就不会有太多的负载。

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'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.我可以使用 StringBuffer 并使用 append 方法连接所有内容(XML 标签和我的数据),然后执行 mystringBuffer.toString().getBytes() 并将字节数组写入输出流。 In this approach I could reuse StringBuffer and ByteBuffer.在这种方法中,我可以重用 StringBuffer 和 ByteBuffer。 Only the bytearray will be a new object each time.只有 bytearray 每次都是一个新的 object。

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.似乎我超越了自己,并假设内存/GC 可能有问题 - 我可能完全错了,仅使用 JAXB 或 XStream 可能是解决方案。 I could then just do some performance testing to find out bottleneck.然后我可以做一些性能测试来找出瓶颈。

Thanks谢谢

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.在看到性能的实际问题之前,不要担心 object 创建开销。 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.) (主要发布这个答案是为了让这个问题得到一些额外的关注/有竞争力的答案,或者至少是简单地将这个从不断增长的未回答问题列表中删除。)

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

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