Question

I am trying to add the object (JavaBean) into the list.

     MyWebServiceRequest mywebService = new MyWebServiceRequest();
     MyRequestType type= new MyRequestType ();

       for(int i=0; i< 9; i++){
           type.setA(someDynamicValue);
           type.setB(someDynamicValue);
           mywebService.add(type);
       }

This creates only one object of (type), so it add same object 9 times with same data.

 MyWebServiceRequest mywebService = new MyWebServiceRequest();
   for(int i=0; i< 9; i++){
           MyRequestType type= new MyRequestType ();
           type.setA(someDynamicValue);
           type.setB(someDynamicValue);
           mywebService.add(type);
       }

This creates multiple objects, add 9 diff object with diff values .

What if (for loop) create hundreds of objects instead of 9 in single loop , each and every time a request is made? So its dump in memory right?

How to avoid this?

Thanks in Advance.

Was it helpful?

Solution

When you do this

 MyWebServiceRequest mywebService = new MyWebServiceRequest();
   MyRequestType type= new MyRequestType ();

   for(int i=0; i< 9; i++){
       type.setA(1);
       type.setB(2);
       mywebService.add(type);
   }

The line mywebService.add(type); adds the object into list , no matter its state is changed or not or it is the same object. Because its a list and not a Map which do not allow duplicates.

So the count goes to 10 , now when you do

 MyWebServiceRequest mywebService = new MyWebServiceRequest();


   for(int i=0; i< 9; i++){
       MyRequestType type= new MyRequestType ();
       type.setA(1);
       type.setB(2);
       mywebService.add(type);
   }

The code now makes a new object and add to list , but every object is different . The only difference in two snippets is , the Objects in the second snippet are not same , but in the first snippet its same but added 10 times.

OTHER TIPS

Because, when you place the object creation outside the loop, only one object will be created. Hence, only that object is added to the list, though you are trying to add it 9 times.

  1. A new object is created Loop Starts
  2. Some values are set into the object.
  3. Object is added to the List.(i.e, referenced in the list)

In second iteration when the values are changed, it is changed in the object created in Step 1 which is referred in the list 10 times.

Hope this clarifies.

MyWebServiceRequest mywebService = new MyWebServiceRequest();
for(int i=0; i< 9; i++){
   MyRequestType type= new MyRequestType ();
   type.setA(1);
   type.setB(2);
   mywebService.add(type);
   type = null;
}

Setting null to the variable 'type' as shown above will not have any impact on memory. Because, the object is already referred to your list. Hence, even if you set null to the variable 'type' it will not have any impact to memory leaks.

Please give an overview of the application and the issue you face if you are facing the issue in a real time application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top