Possible Duplicate:
Creating a memory leak with Java
What’s the easiest way to cause a Java memory leak?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You cannot really "leak memory" in Java unless you:
I take it that you are interested in the last case. The common scenarios are:
A nice example would be to:
StaticGuiHelper.getMainApplicationFrame().getOneOfTheButtons().addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ // do nothing... } })The registered action does nothing, but it will cause the modal window to linger in memory forever, even after closing, causing a leak – since the listeners are never unregistered, and each anonymous inner class object holds a reference (invisible) to its outer object. What’s more – any object referenced from the modal windows have a chance of leaking too.
This is why libraries such as EventBus use weak references by default.
Apart from listeners, other typical examples are caches, but I cannot think of a nice example.