John
John

Reputation: 1189

memory leakage in this code?

private ArrayList<HashMap<String, String>> sortArrayMap(ArrayList arrList)
    {
        ArrayList retArray = new ArrayList();
        Hashtable tableUnOrdered = new Hashtable();

        for (int i = 0; i < arrList.size(); i++)
        {
            HashMap<String, String> map = (HashMap<String, String>) arrList.get(i);
            tableUnOrdered.put(map.get("TCNT"), i);
        }
        Vector v = new Vector(tableUnOrdered.keySet());
        Collections.sort(v);
        for (int j = 0; j < MAX_ITEMS_PER_GRAPH && j < v.size(); j++)
            retArray.add(v.get(j)); // add the list in the needed order

        return retArray;
    }

I am not able to find out where memory leakage is happening in this code, can anyone let me know on this. My boss said this code has memory leakage and asked me to find out.

Upvotes: 1

Views: 397

Answers (4)

KarlP
KarlP

Reputation: 5191

It's broken: I cleaned up the generics, and it's not returning a list of HashMaps, its returning a list of strings.

private ArrayList<HashMap<String, String>> sortArrayMap2(ArrayList<HashMap<String, String>>  arrList)
{
    ArrayList<HashMap<String, String>> retArray = new ArrayList<HashMap<String, String>>();
    HashMap<String, Integer> tableUnOrdered = new HashMap<String,Integer>();

    for (int i = 0; i < arrList.size(); i++)
    {
        HashMap<String, String> map = arrList.get(i);

        tableUnOrdered.put(map.get("TCNT"), i);
    }
    Vector<String> v = new Vector<String>(tableUnOrdered.keySet());
    Collections.sort(v);
    for (int j = 0; j < MAX_ITEMS_PER_GRAPH && j < v.size(); j++)
        retArray.add(v.get(j)); // add the list in the needed order

    return retArray;
}

Upvotes: 1

Brian
Brian

Reputation: 6450

Ask your boss if really means memory leakage, or whether he means it's using too much memory. Tell him there's a big difference and you need to know which it is he's worried about.

Upvotes: 1

Heath Lilley
Heath Lilley

Reputation: 383

Other than the argument all variables live in method scope. They will be marked for garbage collection when the method terminates.

Upvotes: 1

Boris Pavlović
Boris Pavlović

Reputation: 64650

This method as far as I can see doesn't hold references on some enclosing classes fields, therefore it can't cause memory leaking.

Upvotes: 5

Related Questions