Friday, 15 June 2012

java - Will an object in a List be garbage collected? -



java - Will an object in a List be garbage collected? -

i have class looks this:

public class myclass() { private list<thread> threads = new arraylist<>(); ... public myclass() { (int = 0; < 10 ; i++) { thread t = new thread(); threads.add(t); t.start(); } } }

in service, there single instance of myclass hangs around duration service runs. via jconsole, have observed 10 threads created , start doing processing, know set works -- extent.

my problem comes when scheme gets under load. when send several one thousand big objects service, above threads spontaneously die. (as observe in jconsole.) since there's no logic defined tear downwards threads and/or clear threads list, i'm wondering if it's possible java has garbage collected actual objects within of threads list , left kind of weak reference behind.

questions:

under heavy load, possible java garbage collects contents of threads list?

is there way contents of threads list spontaneously go away? eg. threads somehow stopped without explicit code ever calling kind of cleanup logic.

(yes, know poorly designed code, legacy code , i'm trying figure out happening before go seek , prepare implementation.)

under heavy load, possible java garbage collects contents of threads list?

no , no.

no because of them beingness running. running threads don't collected.

and no because of beingness stored in list alive.

put simply, gc never take away chair might want sit down on later.

is there way contents of threads list spontaneously go away?

no. of import question missing:

am doing right?

no. standard way utilize executors, can provide e.g. newfixedthreadpool. unless have special requirements , expertise, stick it. if don't suffice needs, there moreexecutors.

java garbage-collection

No comments:

Post a Comment