Good morning Christian! Dependent Scoped beans get destroyed whenever either the cleanup gets invoked manually or (that's the most important use case) when the CreationalContext it is contained gets destroyed. This is usually the CreationalContext of the NormalScoped bean it got injected into.
Often people make Dependent Scoped beans without thinking about what lifecycle the instances should have. Many times it is plain wrong and they should rather have used @ApplicationScoped instead. I don't know how much contact you already had with the CDI spec, so please excuse if I explain things you already know. But my experience is that people often use advanced technology (CDI, JPA, etc) without ever learning about the very mechanics. Maybe it helps to go back and read that very old (but mostly still valid) article I wrote together with my fellow CDI spec author Pete Muir: https://entwickler.de/java/tutorial-introduction-to-cdi-contexts-and-dependency-injection-for-java-ee-jsr-299-104536 In short: if you do NOT use a @Dependent scoped bean in a NormalScoped bean, EJB, Servlet Filter or any other EE instance which is defined to support CDI, then we also do not store it's CreationalContext. Thus there should also be no mem leak. I'm interested to learn about how your scenario looks like and where the references pile up. Maybe there was some 'workaround' implemented which is really not needed at all if CDI is used properly? txs and LieGrue, strub > Am 22.10.2025 um 09:13 schrieb Christian Ortlepp > <[email protected]>: > > Hey, > > I have a question about the implementation of @Dependent bean destruction in > OpenWebBeans. As per 2.5.4.2. of the cdi spec > (https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0.pdf page 77) > "Finally, the container is permitted to destroy any @Dependent scoped > contextual instance at any time if the instance is no longer referenced by > the application (excluding weak, soft and phantom references).". Is this > implemented in OpenWebBeans, and if so is it enabled by default or do I have > to configure it? > > > My motivation for having something like this is the following: In the > application I am working on @Dependent beans are pretty widely used (I'm > guessing because by using them one didn't have to think about > lifetimes/thread safety so much). The developers that did this however > usually did not remember to call `instance.destroy(bean)` after they were > done using that object. This lead to memory leaks (because the OpenWebBeans > Instance kept references to those objects, as it should if I understood the > spec correctly) and my application has some pretty horrible workarounds in > place to make those memory leaks go away. I would like to get rid of those > workarounds, or at least use a less horrible one. The workarounds we have in > place are also very old and were originally written for (I believe) OWB 1.x. > It may very much be that the current version (which we are using) no longer > needs (some of) the workarounds, my goal is to get a better understanding of > what should be happening. > > > Practically what I'm looking for is a way to declare a bean in a way that if > I get() it via an instance, that instance will immediately forget about the > bean after handing it to me so that the bean can be garbage collected once > I'm done using it. If anybody has an idea how to achieve this either with > built-in means or with extensions I would appreciate any ideas or further > resources. > > > Best, > > Christian >
