Easiest way to cause memory leak in Java? -


possible duplicate:
creating memory leak java

what's easiest way cause java memory leak?

you cannot "leak memory" in java unless you:

  • intern strings
  • generate classes
  • leak memory in native code called jni
  • keep references things not want in forgotten or obscure place.

i take interested in last case. common scenarios are:

  • listeners, done inner classes
  • caches.

a nice example to:

  • build swing gui launches potentially unlimited number of modal windows;
  • have modal window during initialization:
     staticguihelper.getmainapplicationframe().getoneofthebuttons().addactionlistener(new actionlistener(){   public void actionperformed(actionevent e){      // nothing...   } }) 

the registered action nothing, cause modal window linger in memory forever, after closing, causing leak - since listeners never unregistered, , each anonymous inner class object holds reference (invisible) outer object. what's more - object referenced modal windows have chance of leaking too.

this why libraries such eventbus use weak references default.

apart listeners, other typical examples caches, cannot think of nice example.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -