• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Need information about garbage collector in Threads

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic