I have a potential memory leak in my code and am trying to find a solution. I am using the Spring framework for Android. And more specifically
RestTemplate.exchange();
To snap on the fly. However, when I do a memory analysis, I get the following:
1,628 copies of "com.products.Product" downloaded by "dalvik.system.PathClassLoader @ 0x43692b80" occupy 1,363,064 (22.20%) bytes. These instances refer to a single instance of "java.lang.Object []" loaded by "". The dominion tree is as follows:
class com.products.ProductList @ 0x436d7ea8 System class | 1,628 | 8 | 130.240 | 8
mFilteredProducts java.util.ArrayList @ 0x43a4eab0 | 1,628 | 24 | 130.240 | 6,552
array java.lang.Object [1628] @ 0x43bdc888 | 1,628 | 6.528 | 130.240 | 6,528
[274] com.products.Product @ 0x4398b038 | 1 | 80 | 80 | 760
[1175] com.products.Product @ 0x43b26868 | 1 | 80 | 80 | 808
........
The above is a dominant tree. However, I was wondering if there is a safe way to activate the garbage collector. Is an
System.gc();
Safe? However, is there a way to stop this memory leak? The com.products.Product class is just a POJO that will bind JSON fields to the corresponding attributes. Typically, the POJO that is used to bind JSON is as follows:
@JsonIgnoreProperties(ignoreUnknown = true) //must be there all times most likely public class MyPojo { @JsonProperty("Products") private ArrayList<Product> products; public ArrayList<Product> getProducts() { return products; } public void setProducts(ArrayList<Product> products) { this.products = products; } }
com.products.Product:
@JsonIgnoreProperties(ignoreUnknown = true) //must be there all times most likely public class Products { @JsonProperty private String prodnum; @JsonProperty private String brand; @JsonProperty private String name; //get/set }
source share