This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need information about garbage collector in Threads

 
Yogesh Gandhi
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Yogesh Gandhi
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic