Title: Java Observer Pattern — Missing removeObserver Causes OOM
Open Graph Title: Java Observer Pattern — Missing removeObserver Causes OOM
Description: A missing removeObserver caused a 6-hour memory leak in Java Observer Pattern, leading to OOM. Always pair removeObserver with addObserver in dispose.
Open Graph Description: A missing removeObserver caused a 6-hour memory leak in Java Observer Pattern, leading to OOM. Always pair removeObserver with addObserver in dispose.
Opengraph URL: https://thecodeforge.io/java/observer-pattern-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/observer-pattern-java/"
},
"headline": "Java Observer Pattern — Missing removeObserver Causes OOM",
"description": "A missing removeObserver caused a 6-hour memory leak in Java Observer Pattern, leading to OOM. Always pair removeObserver with addObserver in dispose.",
"keywords": "Java Observer Pattern — Missing removeObserver Causes OOM, Java, Intermediate, Advanced Java, A missing removeObserver caused a 6-hour memory leak in Java Observer Pattern, leading to OOM. Always pair removeObserver with addObserver in dispose.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-06T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/java/observer-pattern-java/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java Observer Pattern — Missing removeObserver Causes OOM", "item": "https://thecodeforge.io/java/observer-pattern-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Is java.util.Observable still usable in modern Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "It was deprecated in Java 9 and should not be used in new code. The core problem is that Observable is a class, not an interface, so your subject must extend it — and since Java doesn't support multiple inheritance, that's often impossible. Use PropertyChangeSupport or roll your own interface-based solution instead."
}
},
{
"@type": "Question",
"name": "What is the difference between Observer Pattern and Pub/Sub Pattern?",
"acceptedAnswer": {
"@type": "Answer",
"text": "In the Observer Pattern, observers register directly with the subject — they know about each other through the interface. In Pub/Sub, a message broker sits between publishers and subscribers, meaning publishers and subscribers have zero direct knowledge of each other. Pub/Sub scales better across systems; Observer is simpler and more appropriate for in-process, single-application use cases."
}
},
{
"@type": "Question",
"name": "Can one observer register with multiple subjects at the same time?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, absolutely — and this is actually a common real-world scenario. A single audit logger, for example, might register with a UserService, an OrderService, and a PaymentService simultaneously. Each subject holds its own list of observers independently. The observer just needs to handle the onPriceChanged or propertyChange call correctly regardless of which subject triggered it, which is why the event payload (like PropertyChangeEvent's getSource()) often includes a reference to the originating subject."
}
},
{
"@type": "Question",
"name": "Should I use weak references for observers to avoid memory leaks?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Weak references can help as a safety net, but they are not a substitute for explicit cleanup. If the observer is only held by the subject (and no other strong references), it will be garbage-collected. However, this can cause unexpected unsubscriptions — the observer might disappear in the middle of a notification cycle. It's better to enforce explicit registration/removal via lifecycle methods and use weak references only in specific scenarios like caches or long-lived subjects in frameworks."
}
},
{
"@type": "Question",
"name": "How do I handle exceptions thrown by an observer during notification?",
"acceptedAnswer": {
"@type": "Answer",
"text": "If an observer throws an exception, it will propagate to the subject's notification method and prevent other observers from being notified. To avoid this, wrap each observer call in a try-catch block inside the notifyAllObservers method. Log the exception and continue to the next observer. This ensures one misbehaving observer doesn't break notifications for others. Also consider using an error handler or callback to report failures."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large