Memory-leak-enhance-Worker-thread.md 1.2 KB
Newer Older
1
### Problem 
W
Wing 已提交
2
When using a thread pool, `TraceSegment` data in a thread cannot be reported and there are memory data that cannot be recycled (memory leaks).
3 4 5 6 7 8 9 10 11

### Example
``` java
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setThreadFactory(r -> new Thread(RunnableWrapper.of(r)));
```

### Reason

W
Wing 已提交
12 13
* Worker threads are enhanced when using the thread pool. 
* Based on the design of the SkyWalking Java Agent, when tracing a cross thread, you must enhance the task thread.
14

W
Wing 已提交
15
### Resolution
16

W
Wing 已提交
17
* When using `Thread Schedule Framework`:
W
Wing 已提交
18
See SkyWalking Thread Schedule Framework at [SkyWalking Java agent supported list](../setup/service-agent/java-agent/Supported-list.md), such as Spring FrameWork @Async, which can implement tracing without any modification. 
19

W
Wing 已提交
20
* When using `Custom Thread Pool`:
W
Wing 已提交
21
Enhance the task thread with the following code.
22 23 24 25 26 27 28 29 30

```java
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.execute(RunnableWrapper.of(new Runnable() {
        @Override public void run() {
            //your code
        }
    }));
```
W
Wing 已提交
31
See [across thread solution APIs](../setup/service-agent/java-agent/Application-toolkit-trace-cross-thread.md) for more use cases.
32