简体   繁体   中英

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.

据我所知,此方法在某些封闭的类字段中未保存引用,因此不会导致内存泄漏。

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

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM