使用java中的可变对象将XML数据写入OutputStream
将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同意斯卡夫曼的评论:
所以是的,我还会做一些性能测试,看看是否存在您需要关注的瓶颈。
(发布这个答案主要是为了让这个问题得到一些额外的关注/有竞争力的答案,或者至少,简单地将这个问题从不断增长的未回答问题列表中删除。)
Agreed with skaffman's comment:
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.)