Forums Register Login

Need information about garbage collector in Threads

+Pie Number of slices to send: Send
Dear Friends,

What I know is that if there is no reference that is pointing to a memory, then garbage collector will re-collect the memory according to java norms and documentation.

But, I got confused when I saw the following scenario. Will garbage collection happen in this case as well?






In this case, a never ending thread is created, and started.
As far as my knowledge is concerned, reference to thread created and should be destroyed as soon as it encounters closing bracket of while.

But if it'll release the memory, how the thread will continue to run?

If it'll not release the memory, then is this a case of memory leakage???
+Pie Number of slices to send: Send
Yes, there is a memory leak.

Can someone please tell me the reason for this kind of memory leak.



Throws out of memory after

5087
5088
5089
5090
5091
5092
5093
5094
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:640)
at mypack.XXX.main(XXX.java:17)

+Pie Number of slices to send: Send
 

Yogesh Gandhi wrote:
What I know is that if there is no reference that is pointing to a memory, then garbage collector will re-collect the memory according to java norms and documentation.



A more correct way to think about it is ... if an object is not reachable from a root, then the garbage collector will collect the memory (it may get a bit more complex with weak references, and what happens during the finalizer process, but let's simply ignore those).

Thinking about it as reachable is better because it is possible for an object to have a reference pointing to it, but it is not reachable -- in the case where the object is being referred to by an object that itself is not reachable. Also, thinking in terms of roots is also important, as there are many roots. The stacks of all running threads are roots (including those that hasn't been cleaned up yet). This includes your main thread, threads internal to the JVM, and any threads that you start. Class definitions can be considered as roots (especially since the default class loader won't unload classes for applications). This is because static variables can reach objects. And of course, the JVM has lots of internal data structures that may reach objects.

Yogesh Gandhi wrote:
But, I got confused when I saw the following scenario. Will garbage collection happen in this case as well?






In this case, a never ending thread is created, and started.



Well. I don't need to run it to tell you that you will get an out of memory condition -- most likely running out of stack space needed to create the threads. You can create a ridiculous amount of threads in 10 seconds, so will need a ridiculous amount of memory to survive such a request.

Yogesh Gandhi wrote:
As far as my knowledge is concerned, reference to thread created and should be destroyed as soon as it encounters closing bracket of while.



Remember.... reachable from roots. The started threads can reach there own thread object.

Yogesh Gandhi wrote:
But if it'll release the memory, how the thread will continue to run?

If it'll not release the memory, then is this a case of memory leakage???



Henry
+Pie Number of slices to send: Send
 

Yogesh Gandhi wrote:Yes, there is a memory leak.

Can someone please tell me the reason for this kind of memory leak.



Throws out of memory after

5087
5088
5089
5090
5091
5092
5093
5094
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:640)
at mypack.XXX.main(XXX.java:17)



BTW, in case you didn't notice from the error message. You didn't run out of memory from creating too many thread objects, You ran out of memory from creating too many threads, as there are lots of resources needed to actually start a thread -- the largest one is likely the stack needed for the newly created thread.

Henry
Your mother was a hamster and your father was a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 910 times.
Similar Threads
how can I kill a thread?
thread scope
What happens when a thread completes?
Memory Leak
Thread
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:29:05.