2012-01-23 35 views
0

公共類ProductListGetSet {Eclipse MAT在代碼中顯示momoryleak,此代碼有什麼錯誤?

static ArrayList<ProductListItems> productListItems; 
ProductListItems items; 

ProductListGetSet() 
{ 

    productListItems = new ArrayList<ProductListItems>(); 

} 

public static int getProductListCount() { 

    return productListItems.size(); 
} 

public void setProductListItems(String name, String price, String rating, String review) { 

    items = new ProductListItems(); 
    items.productName = name; 

    items.productPrice = price; 
    items.productRating = rating; 
    items.productReview = review; 

    productListItems.add(items); 

} 

public static ArrayList<ProductListItems> getProductListItems() { 

    return productListItems; 

} 

public void setProductListItemsWithoutReview(String name, String price, String rating) { 

    setProductListItems(name, price, rating, "0"); 

} 

public void setProductListItemsWithoutRating(String name, String price, String review) { 

    setProductListItems(name, price, "0", review); 

} 

public void setProductListItemsWithoutRatingReview(String name, String price) { 

    setProductListItems(name, price, "0", "0"); 

} 

public static void removeProductList() 
{ 
    productListItems.clear(); 
} 

}

我從數據庫中獲取記錄,並保存到這個class.At時候,我會節省超過500條記錄。

有沒有解決辦法或替代方案? 我正在退出內存錯誤()(位圖大小超過虛擬機預算)。 請幫助我!

回答

0

靜態變量productListItems正在構造函數ProductListGetSet中構造。因此,如果我構造ProductListGetSet的實例並使用setProductListItems()添加500個產品,並在我的代碼的另一個點構建另一個ProductListGetSet,則我的代碼將創建另一個productListItems對象。所以原來productListItems的前500名成員在內存中,我正在創建另一個變量副本。

如果您在代碼中構建了很多ProductListGetSet實例,例如在循環中,在虛擬機開始救援之前,可能會導致內存不足。

雖然我覺得還有其他東西會造成更大的傷害,這可能不在您在此發佈的代碼中。

+0

謝謝!!,是的,我從服務器下載圖像(一次500左右),並顯示在一個gridview。在這個活動內存是21299(MemoryInfo信息=新的MemoryInfo(); info.getTotalPss())。但當我回來時它釋放 – JCJ

+0

最初,我的應用程序分配2.757 MB的堆[Heop size爲5.383 MB],同時崩潰的狀態是堆大小爲6.195 MB,分配爲3.37MB。 – JCJ