question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document the need to destroy a bean that was injected via Instance

See original GitHub issue

As written in the post, it is necessary to manually destroy beans created with Instance#get. The current documentation only says

Provides a fully-constructed and injected instance of T.

I would say that “fully-constructed and injected” gives the impression that it is managed by the container. This will cause unobvious memory leaks. I would suggest adding something along the lines of:

The instance is created in the @Dependent(link to the docs of the annotation) scope not managed directly by the container, Therefor, its lifecycle should be handled by the caller. After using it, it should be released with a call to Instance#destroy (or CDI#destroy). Failing to do so can result in a memory leak (usually when the containing bean goes back to the pool/passivated and is not destroyed). A typical use case of such an instance is:

Instance<MyBean> creator = ...;
MyBean myBean = creator.get();
myBean.doSomething();
creator.destroy(myBean);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Ladicekcommented, Jun 23, 2022

Since AtInject’s Provider has no way of destroying a provided instance, I don’t think we should document the need there. If anything, CDI’s Instance can override that method for the purpose of extending the javadoc.

1reaction
Ladicekcommented, Jun 23, 2022

I think the issue in AtInject is incorrect, as Instance belongs to CDI, but maybe I got confused?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invoke spring bean init and destroy methods? - Java2Novice
You can define initialization and destroy methods with in the spring bean. You can configure it using init-method, and destroy-method in the xml...
Read more >
Spring - Bean Life Cycle - Tutorialspoint
Similarly, destroymethod specifies a method that is called just before a bean is removed from the container.
Read more >
2.2. @Bean - Spring
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method...
Read more >
Spring - init() and destroy() Methods with Example
Spring will call this method just before destroying the bean. destroy-method is used to release resources or perform some destruction task.
Read more >
Introduction to Contexts and Dependency Injection - Quarkus
In our example above, the Translator bean has two bean types: Translator and java.lang. ... Your can use programmatic lookup via javax.enterprise.inject.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found