Memory Leak, Spring for Android

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 } 
+6
source share
2 answers

The call to System.gc() is safe. But this does not necessarily cause the actual garbage collection. Calling this method allows the JVM to make every effort to perform garbage collection. In Java, there is no way to force it.

Regarding memory leak. Take a look at ProductList , why it holds all these objects. If ProductList is still referenced anywhere in your application, all of these products will not be released.

0
source

System.gc () is safe, as @wajda already said

Today I had a similar problem with a desktop application, and the problem was that after use I did not close the PreparedStatement and ResultSet objects. After about 600 requests, I filled 256 MB of allocated space. After using JVisualVM (you should have it in your JDK) and analyzing what is accumulating, I saw char arrays occupying 80% of the space, and they were all empty.

Try to close all JSON objects after each use and see if it helps.

Greetings

0
source

All Articles