简体   繁体   English

内存泄漏,适用于Android的Spring

[英]Memory leak, Spring for Android

I have a potential memory leak at my code and I'm trying to find a solution. 我的代码中存在潜在的内存泄漏,我正在尝试找到解决方案。 I'm using the Spring for Android framework. 我正在使用Spring for Android框架。 And more specific the 而且更具体了

  RestTemplate.exchange();

In order to make an on the fly binding. 为了使飞行绑定。 However, when i make a memory analysis I get the following: 但是,当我进行内存分析时,我得到以下内容:

1.628 instances of "com.products.Product", loaded by "dalvik.system.PathClassLoader @ 0x43692b80" occupy 1.363.064 (22,20%) bytes. 1.628个“com.products.Product”实例,由“dalvik.system.PathClassLoader @ 0x43692b80”加载,占用1.363.064(22,20%)个字节。 These instances are referenced from one instance of "java.lang.Object[]", loaded by "". 这些实例是从“java.lang.Object []”的一个实例引用的,由“”加载。 The dominator tree is the following: 支配树如下:

class com.products.ProductList @ 0x436d7ea8 System Class| class com.products.ProductList @ 0x436d7ea8 System Class | 1.628 | 1.628 | 8 | 8 | 130.240 | 130.240 | 8 8

mFilteredProducts java.util.ArrayList @ 0x43a4eab0| mFilteredProducts java.util.ArrayList @ 0x43a4eab0 | 1.628 | 1.628 | 24 | 24 | 130.240 | 130.240 | 6.552 6.552

array java.lang.Object[1628] @ 0x43bdc888| array java.lang.Object [1628] @ 0x43bdc888 | 1.628 | 1.628 | 6.528 | 6.528 | 130.240 | 130.240 | 6.528 6.528

[274] com.products.Product @ 0x4398b038| [274] com.products.Product @ 0x4398b038 | 1 | 1 | 80 | 80 | 80 | 80 | 760 760

[1175] com.products.Product @ 0x43b26868| [1175] com.products.Product @ 0x43b26868 | 1 | 1 | 80 | 80 | 80 | 80 | 808 808

........ ........

The above one is the dominator tree. 以上是主宰树。 However I was wondering if there is a safe way to activate the garbage collector. 但是我想知道是否有一种安全的方式来激活垃圾收集器。 Is the 是个

  System.gc();

Safe? 安全? However,is there a way to stop taking this memory leak? 但是,有没有办法停止内存泄漏? The class com.products.Product is just a simple POJO which will bind the JSON fields to the corresponding attributes. com.products.Product类只是一个简单的POJO,它将JSON字段绑定到相应的属性。 Generally the POJO that is used to bind the JSON is as following: 通常,用于绑定JSON的POJO如下:

@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: 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
}

Calling System.gc() is safe. 调用System.gc()是安全的。 But it doesn't necessarily trigger the actual garbage collection. 但它并不一定会触发实际的垃圾收集。 Calling this method only suggests JVM to make all the efforts to perform garbage collection. 调用此方法仅建议 JVM尽一切努力执行垃圾回收。 There is no way in Java to force it though. Java中没有办法强迫它。

Regarding the memory leak. 关于内存泄漏。 Look at the ProductList , why is it holding all those objects. 查看ProductList ,为什么它包含所有这些对象。 If ProductList is still referenced from anywhere in your application all those products will not be released. 如果仍然从应用程序的任何位置引用ProductList,则不会发布所有这些产品。

System.gc() is safe as @wajda already said System.gc()是安全的,因为@wajda已经说过了

Today I had similar problem with desktop application and the problem was that I wasn't closing PreparedStatement and ResultSet objects after usage. 今天我遇到了与桌面应用程序类似的问题,问题是我在使用后没有关闭PreparedStatement和ResultSet对象。 After about 600 queries I filled up 256mb of allocated space. 在大约600次查询之后,我填充了256mb的分配空间。 After using JVisualVM (you should have it in your JDK) and analyzing what is piling up, I saw char arrays taking 80% of space and they were all empty. 在使用JVisualVM(您应该在JDK中使用它)并分析堆积的内容之后,我看到char数组占用了80%的空间并且它们都是空的。

Try and close all JSON objects after each usage and see if it helps. 每次使用后尝试并关闭所有JSON对象,看看它是否有帮助。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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