Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:32:41+00:00 2026-05-27T17:32:41+00:00

What is the best way to write XML to an OutputStream (TCP Socket) without

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:32:41+00:00Added an answer on May 27, 2026 at 5:32 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to write some values into a xml file?
What is the best way to write data to the text console at arbitrary
What's the best way to write a LINQ query that inserts a record and
What is the best way to write bytes in the middle of a file
What's the best way to write a supybot plugin in ruby? Or other language
What is the best way to write a function (or something DSLish) that will
What's the best way to write an application in order to get it working
What would be the best way to write log statements to a file or
What would be the best way to write a generic copy constructor function for
Following on from this question what would be the best way to write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.