how to use less memory for java loop operations

I have this for loop below that uses up a lot of memory because of the large amount data. The column1 and column2 variables are ArrayList <String> . Is there a less usage of memory to accomplish the same for task as below?

for (String columnA : column1)
      for(String columnB : column2
          String.out.println(columnA + " " + columnB);

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm afraid that there's no way we can do that task better than the above solution.
the only thing you can optimize is here String.out.println(columnA + " " + columnB);

StringBuilder builder = new StringBuilder() ;
for (String columnA : column1)
      for(String columnB : column2
          String.out.println( builder.append( columnA ).append( " " ).append( columnB ) );
           builder.delete( 0, builder.length() ) ;

the above code would avoid creation of StringBuilder for each sop!
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

areyouready344Author Commented:
Ksivananth, this will reduce the memory usage?
ofcourse yes but not sure how much... try it
Where do column1 and column2 come from?

If you have issues with the memory, the easiest way to fix them would be to somehow make these smaller -- if you read them from somewhere, just read them in chunks. Otherwise you are kinda stuck...

The String to StringBuffer will help somewhat (because every time you use + with a String, it copies the string...) but these strings should get released and caught from the gc at one point...
>>because every time you use + with a String, it copies the string

not really the case from 1.5, they are replaced with StringBuffer( 1.5) or StringBuilder( 1.6 ) by the compiler...
Right, And the code is obviously not earlier one (good catch - dunno really what I was thinking about there.)
Yes.. if you want your program to be thread safe then use StringBuffer else you can use StringBuilder.
the concept of not using String is that during concatination..String internally converts its value in to a StrinBuffer and then apply .apend() function on it and then converts back to String..
so there is un necessary overhead of conversion twice..
String-->StringBuffer    and     StringBuffer-->String

So Not using Strings in case of concatination will decrease the execution time for sure.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.