1

I was creating 16 UN-necessary object (of no use) in my Action class.

List<String> list = new ArrayList<String>();

My application is customer portal which is used by world wide users .

and that action class is accessed many times i believe in a day.

So My question is how much these 16 objects could effect the memory ? a little ? Or too much ?

skiwi
  • 66,971
  • 31
  • 131
  • 216
gSingh
  • 57
  • 1
  • 14
  • I just asked to know how much memory created for Object types : http://stackoverflow.com/questions/22932251/how-much-memory-allocates-to-element-in-object – Suresh Atta Apr 08 '14 at 09:43
  • 2
    Why would you create these instances in the first place, if they remain unused? – Stefan Apr 08 '14 at 09:47
  • Did you get an OutOfMemoryError or did you realized that your server runs out of memory? Or why do you fear that these object use up your memory? The empty List would be around ~1KB of memory. – Absurd-Mind Apr 08 '14 at 09:50
  • @Stefan i have created them unknowingly....like when i was casting my object from session than i was creating a new object again...like this List list = new ArrayList(); and than i was doing... list=(List) session.gettAttribute("myList") ; where myList is the object which i created in my earlier class..... – gSingh Apr 08 '14 at 09:50
  • i was doing this wrong casting 16 times ...so i asked how much it will effect . I didnt get any OOM error... – gSingh Apr 08 '14 at 09:53

1 Answers1

2

An empty ArrayList costs 88 Bytes on a 32-bit system. See this explanation for the calculation.

So just multiply this with 16 and the number of times your action is called. But ever better would be if you find a solution where you would not need to create unnecessary list objects. Most of the time this is should not be needed.

Luke
  • 283
  • 2
  • 10
  • i think this answer is for `ArrayList`, not `ArrayList`. `new ArrayList()` only save it's reference() when `new ArrayList()` save the empty string. An empty String takes 40 bytes—enough memory to fit 20 Java characters. When object reference only take 4bytes for 32 bit JVM. it's mean the size will be 10 times more than `ArrayList`. – Angga Apr 08 '14 at 10:24
  • ArrayList stores its element in an array which only stores references to the containing elemements. I think there is no difference between an empty ArrayList and an empty ArrayList when it comes to memory. – Luke Apr 08 '14 at 10:31