# Question about agent detachment

**URL:** <https://discuss.elastic.co/t/question-about-agent-detachment/330650>\
**Category:** APM\
**Tags:** java\
**Created:** [April 24, 2023, 12:41pm UTC](https://discuss.elastic.co/t/question-about-agent-detachment/330650 "2023-04-24T12:41:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dogourd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dogourd/32/120143_2.png) [@dogourd](https://discuss.elastic.co/u/dogourd)\
**Post date:** [April 24, 2023, 12:41pm UTC](https://discuss.elastic.co/t/question-about-agent-detachment/330650/1 "2023-04-24T12:41:39Z")

</div>

I noticed there is a `detach` method in `co.elastic.apm.agent.premain.AgentMain` which seems to be used for detaching the agent, although the comment says it's only for demonstration purposes.  
I tried to use this method to detach my Java agent and found that when the target application is using Java versions below 11, the class loader cannot be unloaded properly. In Java 8, this results in Metaspace constantly expanding, even when full GC occurs.

With multiple alternations of attach-detach, the metaspace looks like this.

 ![1682338547143_7E170010-6445-4862-997B-1F669D8ADD71](https://us1.discourse-cdn.com/elastic/original/3X/2/e/2e22cf169578a1acabb338a09f6ce22e7952b9da.png)

My question is whether the elastic-apm-agent has ever validated the detach method here? Does it work properly?

I have written an [example](https://github.com/dogourd/agent-detach-issue) that reproduces the problem I described, and my current conclusion is that once the `IndyBootstrap#bootstrap` method is executed at runtime, the custom class loader cannot be unloaded. However, it's worth noting that if our application is running on Java 11 or higher, class unloading can be completed successfully. Does anyone have any insight into this issue?

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [April 24, 2023, 6:00pm UTC](https://discuss.elastic.co/t/question-about-agent-detachment/330650/2 "2023-04-24T18:00:06Z")

</div>

Per the comment, it's not even partially complete and so I'm unsurprised to see your results. At the current time we don't have this on our roadmap to put any resources into it. We're happy for others to work on it, there are several suggestions in the comment as to what more needs doing

---

<div class="post-metadata">

**Author:** ![dogourd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dogourd/32/120143_2.png) [@dogourd](https://discuss.elastic.co/u/dogourd)\
**Post date:** [April 26, 2023, 11:24am UTC](https://discuss.elastic.co/t/question-about-agent-detachment/330650/3 "2023-04-26T11:24:23Z")

</div>

Hi, @Jack_Shirazi:  
I noticed the comment from Elastic on this method, and as far as I understand, we need to do the following:

1. Separate the agent core as a standalone jar, instead of having it in the `agent/` folder within the javaagent.
2. Make sure that `ClassFileTransformer` is removed and classes are retransformed.
3. Stop all threads within the javaagent.
4. Ensure that we use `DetachThreadLocal` to replace all `ThreadLocal`s and clean them up.
5. Close custom class loaders.

In addition, I also set `bootstrap` to null in `InvokeBootstrapDispatcher`, but even after doing all of the above, as I mentioned earlier, we still can't unload it in Java 8, 9, and 10 runtime environments. However, if it runs on Java 11 and higher versions, the unloading can be completed perfectly. I've been troubleshooting this issue for days and I'm almost stuck here. To this end, I created a minimal example repository [agent-detach-issue](https://github.com/dogourd/agent-detach-issue) to demonstrate and reproduce this issue, and uploaded a heap dump there. If you could spare some time, I hope you could take a look at it. I'm starting to suspect it's a bug in versions before Java 11.

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [May 3, 2023, 12:25pm UTC](https://discuss.elastic.co/t/question-about-agent-detachment/330650/4 "2023-05-03T12:25:49Z")

</div>

I'm sorry, we're not going to be able to prioritize time to look into this

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 24, 2023, 8:26am UTC](https://discuss.elastic.co/t/question-about-agent-detachment/330650/5 "2023-05-24T08:26:08Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
