• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList ensureCapacity  RSS feed

 
Atul Dawkhare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please reply me on this? I have submited the code which is failing due to some reason.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!