Skip to Content
author's profile photo Former Member
Former Member

Possible of memory leak in the loop

Recently my application do get OutOfMemory issue. I realized the memory is keep on stack up as i saw in the task manager, the jlaunch keep growing and it won't drop back. That day i'm running a search function and it will query the table to retrieve the data. The jlaunch shoot from 500MB -> 2.2GB and now remain in there. Wondering is it during it query it populate at least 10,000 records into the arraylist and then the memory already allocated and once i finish run the function, it will clear the allocated memory to re-use it.

public ArrayList ejbHomeInJDBCConnection(Map map){
	ArrayList beanList = new ArrayList();
		Context ctx = new InitialContext();
		DataSource ds = (DataSource) ctx.lookup("jdbc/POOL");
		Connection con = ds.getConnection();
		String query = "SELECT * FROM USER WHERE ";
		for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
			Object key =;
			if(key.toString().startsWith("TIME") | key.toString().startsWith("TIMEIN")){
				long longValue = Long.parseLong(map.get(key).toString());
				query += key.toString()+ longValue + " AND "; 	
				String value = (String)map.get(key);
				query += key.toString()+ value + " AND "; 	
		String newquery = query.substring(0, query.length()-5);
		newquery += " ORDER BY TIMEIN DESC";
		Statement stmt = con.createStatement();
		try {
			ResultSet rs = stmt.executeQuery(newquery);
		    try {
				while ({
					InBean bean = new InBean();
	}catch(Exception e){
return beanList;

Wondering is it the InBean will cause any memory leak as if there is 10,000 records, which mean it will create 10,000 objects and once it add into the arraylist the previous bean is not in use, will the GC clear it as i didn't set it as null. Do i need to do something like reallocate/defragment the memory?


Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    Posted on Sep 15, 2009 at 06:22 AM

    hi chan,

    might be the memorry issue,

    if we storing the single interger value, problem may not occur, if we store the object(with some data members)-- my level of understiandig-- coz i do't know the what is the bean returing (IBEAN)

    might be we have to clear the meomroy or restart the jlaunch instance every time

    let me know much i understand u r problem


    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member


      I'm sure a "count" would not generate the overhead you are concerned.

      To understand some aspects, you need to read the source files of Java, and understand how the stack would work in your case.

      Evertime you "add" an element to you list, the implementation will run the ensureCapacity, and grown the list one by one. Understand that the list is an Array with a lot more functions, but below, you are still working with an Array, and it needs to have a defined size. Everytime you add, it's doing a System.arraycopy(all the crap) - So you can save this, everytime you add something if you create your List with the right size.

      Note, this is not an issue if we consider small lists, of small objects, but working with large lists, you can feel slow downs.

      About the GC stuff, well.. I'm sure you can do some reading how it works. One good start point would be

      Link: []

      I'm sure you don't need that, but still, it's good reading. Maybe you should just increase your heap size, or you can manually clear the List using list.clear();



  • Posted on Sep 17, 2009 at 09:33 AM

    Hi Adrian

    1. I do not see Connection.close() method in your code. I'm not sure if it's strongly necessary, but try to close connection after use. It'll release JDBC resources.

    2. I think the following code is not efficient if you are making the same SQL many times:

    Statement stmt = con.createStatement();

    Official Javadoc says the following about the method:

    >If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.

    I suggest you to try

    PreparedStatement Connection.prepareStatement(String sql);


    > A SQL statement with or without IN parameters can be pre-compiled and stored in a <code>PreparedStatement</code> object. This object can then be used to efficiently execute this statement multiple times.

    BR, Siarhei

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.