You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "duongkame (via GitHub)" <gi...@apache.org> on 2024/03/01 01:43:35 UTC
Re: [PR] HDDS-9848. Pipeline.nodesInOrder should not be ThreadLocal [ozone]
duongkame commented on PR #5944:
URL: https://github.com/apache/ozone/pull/5944#issuecomment-1972295726
I'm not sure that the memory leak is real because I cannot find it in any Java official document. I created a simple test to see if it leaks values, in different conditions, i.e. when objects are created, and it looks to me that no ThreadLocal values are leaked. All ThreadLocal values are cleaned up when the wrapping object is GCed.
```
public class TestThreadLocal {
public static void main(String[] args) throws InterruptedException {
testWithNewThread(5);
System.gc();
Thread.sleep(1000);
// At this point, ThreadLocal values with the last thread is release.
System.out.println("Complete 1st thread.");
// Want to when thread comes and go, ThreadLocal values are released.
testWithNewThread(5);
System.gc();
Thread.sleep(1000);
}
static void testWithNewThread(int n) throws InterruptedException {
Thread t = new Thread(() -> {
for (int i = 0; i < n; i++) {
// create an object with ThreadLocal data and discard it.
new WithThreadLocal().putInThreadLocal();
}
});
t.start();
t.join();
}
static class WithThreadLocal {
private ThreadLocal<ThreadLocalData> dataHolder = new ThreadLocal<>();
private void putInThreadLocal() {
dataHolder.set(new ThreadLocalData());
}
}
static class ThreadLocalData {
private final static AtomicInteger COUNTER = new AtomicInteger();
private final int id = COUNTER.incrementAndGet();
@Override
protected void finalize() throws Throwable {
System.out.println(id + ": I am collected by GC!");
}
}
}
```
```
5: I am collected by GC!
4: I am collected by GC!
3: I am collected by GC!
2: I am collected by GC!
1: I am collected by GC!
Complete 1st thread.
10: I am collected by GC!
9: I am collected by GC!
8: I am collected by GC!
7: I am collected by GC!
6: I am collected by GC!
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org