aspose file tools
The moose likes Java in General and the fly likes ArrayList ensureCapacity Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "ArrayList ensureCapacity" Watch "ArrayList ensureCapacity" New topic
Author

ArrayList ensureCapacity

Atul Dawkhare
Greenhorn

Joined: Jul 06, 2011
Posts: 4
Hi ,
I am new to this forum. Please guide me if anything goes wrong from my side.

My queries is that
in my web app. I am getting error that java.util.ArrayList.ensureCapacity(ArrayList.java:222)
I Dont know more on this. I googled and found that there is one method which increase the capacity of an arraylist. But I want to know what is current capacity after putting data. So that I will increase the capacity by calling ensureCapacity Method to avoid the error.

Also want to know after this error , I am getting outof memory error. Is there any relation between these tow.

Please guide me.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

the default capacity of an ArrayList is 10. but it can grow dynamically as need basis. you should call ensureCapacity, only when you need to add large amount of data and you know the amount of data.

and I dont think there is a link between ensureCapacity and the error what you are getting. probably you need to increase your Heap memory while running your program. just google for java -Xmx command option
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi, can you post some code which leads to the out of memory error. Might be there is a never ending loop or string concatenations in the code which may cause this.

And welcome to the Ranch
Atul Dawkhare
Greenhorn

Joined: Jul 06, 2011
Posts: 4
Hi,

First of all thannks for the reply.

Here is the code.
The basic functionality of the function is , It fetches records from Database and put it into arraylist. Now for some queries there might be larger amout of data getting fetched from database. and that is why getting error ensureCapacity.
So I want to know what is the capacity at the time of filling data into arraylist so that I can pass the correct number [min capacity] to the function.

ArrayList list = new ArrayList();

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
con = getConnection();
ps = con.prepareStatement(sql);
for (int x = 0; x < params.length; x++) {
Object param = params[x];
if (param == null) {
param = "";
}
ps.setObject(x + 1, param);
}
rs = ps.executeQuery();
int colCount = rs.getMetaData().getColumnCount();

while (rs.next()) {
Object[] row = new Object[colCount];
for (int x = 0; x < colCount; x++) {
row[x] = rs.getObject(x + 1);
}
list.add(row);
}


And after this only I get Out of Memory error. yes this error is random and I am not able to replicate on my local machine.

Atul Dawkhare
Greenhorn

Joined: Jul 06, 2011
Posts: 4
Can anybody please reply me on this? I have submited the code which is failing due to some reason.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Seetha - probably you need to increase your Heap memory while running your program. just google for java -Xmx command option

Did you try what he said? can you try set like -Xms1024M.
Atul Dawkhare
Greenhorn

Joined: Jul 06, 2011
Posts: 4
No, by executing the code with -Xms1024M will might solve the problme of out of memory [Please correct me if I am wrong]..
But I want to know more on the ensuareCapacity. Let me repeat my query again.

Facing error something like

java.util.ArrayList.ensureCapacity(ArrayList.java:222)

I want to know what is current capacity after putting / filling data in arrayList. So that I will increase the capacity by calling ensureCapacity Method to avoid the error.

Please reply me on more on this
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
i dont know about ensure capacity. check if these help
http://www.coderanch.com/t/374065/java/java/ArrayList-ensureCapacity
http://www.coderanch.com/t/374568/java/java/arrayList-ensureCapacity
http://www.coderanch.com/t/536331/java-programmer-SCJP/certification/help-ensureCapacity-function-ArrayList http://www.coderanch.com/forums/forums/posts/watch/0/434178
http://www.coderanch.com/t/376873/java/java/ensureCapacity-boost-arrayList-performance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList ensureCapacity
 
Similar Threads
Vector Over ArrayList in term of Capacity..!!
ensureCapacity() method in ArrayList
In the API definition Of ArrayList what does E mean
arrayList.ensureCapacity()
ArrayList.ensureCapacity