gmhk
gmhk

Reputation: 15940

Difference between the List and Enumeration

I have a written a method to get all the records and return in the List Type, but I got out of memory error. So I changed return type from List to Enumeration, in the method, instead of ArrayList, uses Vector and return vector.elements at the end of the method. It works without any error. but I did not understand why.

Could someone explain Why this Enumeration worked?

Upvotes: 4

Views: 6043

Answers (2)

user207421
user207421

Reputation: 310957

You must have fixed something else. A Vector will use if anything more memory than an ArrayList, and returning an Enumeration instead of the list itself only adds a tiny bit more memory usage, unless your caller was using a list iterator in which case it is line ball. There's certainly no reason for this strategy to use significantly less memory.

Unless you were returning a copy of the original list? as a new ArrayList? That would double the memory usage at least while the copy was being made, but it would have to be a very long list ...

Upvotes: 0

Bozho
Bozho

Reputation: 597124

Enumeration is the "old version" of Iterator.
Vector is the "old version" of ArrayList.

The memory difference is not supposed to be significant, so perhaps the fluctuations you've observed are due to another thing.

Depending on the size of the list you may need to increase the maximum memory of the JVM (using Xmx, Xms and/or XX:MaxPermSize)

Upvotes: 5

Related Questions