0

How are these code snippets different from each other:

Snippet 1: HashMap map1= abc() ;

Snippet 2: HashMap map2= new HashMap();
           map2 = abc();

I'm getting OutOfMemoryError in my application. Can the reason of this error due to the implementation of Snippet 1 in my code?

Edit: Added implementation of abc()

public HashMap abc(){
    HashMap  rMap = null;

    StringBuffer  sQuery = new StringBuffer("");

     sQuery.append(" SELECT DISTINCT ABC ,DEF, ");
     sQuery.append(" XYZ, ID, NAME  ");
     sQuery.append(" FROM TABLE1");

    Query query = new Query( sQuery.toString());

    List rList = query.executeSelect();

    if (rList != null && rList.size() > 0) {
        Iterator listIter = rList.iterator();
        Map map = null;
         rMap = new HashMap();
        while (listIter.hasNext()) {
            map = (HashMap) listIter.next();

            String key = map.get("ABC") + "%"+ map.get("DEF")+"%"+map.get("XYZ");
            if( rMap.containsKey(key)){
                LinkedHashMap sMap = (LinkedHashMap) rMap.get(key);
                sMap.put(map.get("ID"), map.get("NAME"));
                 rMap.put(key, sMap);

            }else{
                LinkedHashMap sMap = new LinkedHashMap();
                sMap.put(map.get("ID"), map.get("NAME"));
                 rMap.put(key, sMap);
            }
        }
    }
    return  rMap;
}
KLCoder
  • 79
  • 3
  • 11
  • 1
    NO. Sorry for the long comment :P – Fran Montero Aug 04 '15 at 10:42
  • 2
    Snippet 2 creates additional HashMap instance that is never used (which means Snippet 1 is better), but without seeing the code of `abc()` it's hard to tell what's causing your OutOfMemoryError. – Eran Aug 04 '15 at 10:42
  • At the time `map2 = abc()` is exectuted, first `HashMap`in snippet 2 is elegible for garbage collection, so that's not the problem. – Fran Montero Aug 04 '15 at 10:44
  • abc() returns a HashMap of type HashMap> – KLCoder Aug 04 '15 at 10:46
  • @KLCoder That's not enough information. How often are you calling abc? Does each call to abc produce a new HashMap, or does it return the same instance in each call? Can you show the code of abc? – Eran Aug 04 '15 at 10:51
  • How many rows are in the `TABLE1` table? – Mick Mnemonic Aug 04 '15 at 12:06

3 Answers3

1

In Snippet 2 you create an extra instance which would be eligible for garbage collection on the next statement. So basically they're the same and it shouldn't cause a memory leak.

Try running a profiler to check memory map or increase the heap size.

dogant
  • 1,336
  • 1
  • 10
  • 23
0

Your problem seems to be in another part of your code. Try setting an automatic heapdump when a OutOfMemoryError occurs. See this Using HeapDumpOnOutOfMemoryError parameter for heap dump for JBoss

Then, fire the exception running your code.

Last thing is to analyze the dump with a tool like Eclipse Memory Analyzer https://eclipse.org/mat/ and look for big objects o a big number of instances of small objects. There are lots of tutorials about how to analyze a memory dump: http://www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html

Community
  • 1
  • 1
Fran Montero
  • 1,581
  • 10
  • 21
0

Basically in snippet 2 at the beginning you just initialize the map2 for nothing. After second step it will just point to another address and older one will be garbage collected.