Which is more expensive and by how much:
List<cards> cardList = getMyList(small);//returns a list of 100 elements
cardList.add(card);
Vs.
List<cards> cardList = getMyList(big);//returns a list of 100,000 elements
cardList.add(card);
I guess my real question is, is it expensive to bring a large list into memory? Or is list smart enough to get only as big as it needs to be? Small when adding but large when searching.
Well, obviously, getting a big list in memory is more expensive than getting a smaller one.
In fact, the cost factor depends upon the size of your objects, and upon the initial heap size. Indeed, when JVM has no more memory, it doubles its heap size from its Xms parameter up to its Xmx parameter.
However, this is only true if the
getMyBigListmethod creates the objects. If these objects are already loaded in memory, this method will only load a list of 100 000 references in memory, which won’t cost you more than a few Mb.In such a case your limiting factor won’t be memory allocation for the JVM, but rather the method you use to load that list.
Are they loaded from network ? the bandwidth is then the limit.
Are they loaded from a magnetic hard disk drive ? the bandwidth is then the limit.