简体   繁体   English

如果我们多次创建HashMap对象,则会发生内存泄漏

[英]Memory leak if we create HashMap object multiple times

public class TestProcessor{
  public void fillData(){
    boolean success = true;
    HashMap<String,String> hMap = null;
    if (success){
      hMap = new HashMap<String,String>();
      hMap.put("one","java");
      hMap.put("two","servlet");
   }
   if(hMap! = null){
     processData(hMap);
   }
 }
 public void processData(HashMap<String,String> map){
     String param1 = map.get("one");
     String param2 = map.get("two");
 }
}

In the above code if we call fillData() method multiple times and the if condition becomes true then HashMap object will be created multiple times.Will this cause Memory Leak problem?If memory leak happens then how can we fix it? 在上面的代码中,如果我们多次调用fillData()方法并且if条件变为true,那么HashMap对象将被创建多次,这会导致Memory Leak问题吗?如果发生内存泄漏,那么我们该如何解决呢?

It should not create a memory leak as you will be replacing your existing hashmap which would allow the old one to be garbage collected. 它不应该造成内存泄漏,因为您将替换现有的哈希图,这将使旧的哈希图被垃圾回收。

If you are holding references to the objects within in the hashmap externally, then you may cause them to be retained. 如果您在外部保留对哈希图中内部对象的引用,则可能导致它们被保留。

*I'm assuming this is java from your syntax. *根据您的语法,我假设这是java。

The Java Virtual Machine (JVM) actively and automatically manages the memory your application uses. Java虚拟机(JVM)主动并自动管理您的应用程序使用的内存。 Some things to keep in mind about Java's memory management: 关于Java的内存管理,请记住以下几点:

  • Memory is automatically allocated in heap memory for objects that your program creates. 内存会自动在堆内存中分配给程序创建的对象。

  • When an object can no longer be accessed by your program (usually by falling out of scope so that no variables that reference the object can be accessed) the memory is automatically reclaimed by a process called garbage collection. 当程序无法再访问对象时(通常是由于超出范围而导致不能访问引用该对象的变量),内存将被称为垃圾回收的进程自动回收。

  • Garbage collection is automatic and non-deterministic. 垃圾收集是自动的且不确定的。 Your program can't know (or predict) when garbage collection is going to occur (so you don't know exactly when unreachable objects will be reclaimed). 您的程序无法知道(或预测)何时会发生垃圾回收(因此您不确切知道何时将回收无法访问的对象)。

In the majority of cases, the JVM manages memory so well that no memory leaks occur even when a program runs for a long time (and creates and reclaims many objects). 在大多数情况下,JVM很好地管理内存,即使程序长时间运行(并创建和回收许多对象),也不会发生内存泄漏。

By the above reasoning, the code snippet you show will not result in any memory leaks. 根据上述理由,您显示的代码段不会导致任何内存泄漏。 There are only a few situations in which memory leaks will occur in Java: 在Java中,只有少数几种情况会发生内存泄漏:

1. When you do your own memory management. 1.执行自己的内存管理时。 Memory leaks can occur if you implement your own data structures for objects. 如果您为对象实现自己的数据结构,则可能发生内存泄漏。 For example, if you create your own stack implementation, objects that are "popped" out of your stack can still have active references to them. 例如,如果您创建自己的堆栈实现,则从堆栈中“弹出”的对象仍然可以对其进行有效引用。 When this happens, the object will not be garbage collected even if it is no longer in the active portion of your stack. 发生这种情况时,即使对象不再位于堆栈的活动部分中也不会被垃圾回收。 This is one of the few cases in which it can be important to actively assign null to elements that may refer to objects that are no longer "being used." 这是为数不多的情况之一,在这种情况下,将null主动分配给可能引用不再“正在使用”的对象的元素可能很重要。

2. Any time you have a long-lived object that holds a reference to an object that you intend to be short lived. 2.任何时候只要您有一个长期对象,而该对象持有对您打算短暂存在的对象的引用。 The most common situation in which this can cause memory leaks is in the use of non-static inner classes and anonymous classes (both of which contain a reference to their enclosing instance). 可能导致内存泄漏的最常见情况是使用非静态内部类和匿名类(它们均包含对其封闭实例的引用)。

Every non-static Inner Class has an implicit reference to its surrounding class. 每个非静态内部类都有对其周围类的隐式引用。 Anonymous Classes are similar. 匿名类是相似的。 To successfully create a memory leak simply pass an Inner Class object to a method which keeps references to the provided objects and you're done. 要成功创建内存泄漏,只需将内部类对象传递给保留对所提供对象的引用的方法即可。

Why does this cause a memory leak? 为什么这会导致内存泄漏? Suppose you implement something like a cache. 假设您实现了类似缓存的功能。 Next follow the execution path to a local object which stores some of its inner class objects into the cache. 接下来,沿着执行路径到达本地对象,该对象将其一些内部类对象存储到缓存中。 After the local object was out of scope, it won't be garbage collected anymore! 本地对象超出范围后,将不再进行垃圾收集! The inner class object in the cache holds a reference to the surrounding object and that one is still referenceable and therefore not a candidate for garbage collection anymore. 缓存中的内部类对象持有对周围对象的引用,并且该引用仍然是可引用的,因此不再是垃圾回收的候选对象。 The same is true for anonymous classes! 匿名类也是如此!

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

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