Results 1 to 2 of 2

Thread: garbage collection anonymous objects?

  1. #1

    Default garbage collection anonymous objects?

    Here's a weird thought. If I create an anonymous object then put that
    object to some use, could it still be garbage collected? In particular,
    given the following code, I don't see what guarantees that the thread
    created won't be garbage collected.

    Qt Code:
    1. public class ServerTest implements Runnable
    2. {
    3. public static void main(String[] args)
    4. {
    5. new Thread( new ServerTest() ).start();
    6. }
    7.  
    8. public void run()
    9. {
    10. while( true )
    11. { //....
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    I didn't run this so sorry for any missed errors, but you get the idea.
    Shouldn't the JVM garbage collect the thread at some point? This code
    seems like trouble waiting to happen, but I'm sure I've seen this exact
    idiom elsewhere.
    Last edited by d_stranz; 31st July 2025 at 15:59.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,329
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: garbage collection anonymous objects?

    Excuse me, but why are you posting a question about the JVM in a forum devoted to Qt, a C++ programming framework?

    You are more likely to get an answer to your question if you find a forum that covers Java and the JVM.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Qt closeevent and garbage collection
    By ehnuh in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2012, 16:36
  2. QList and garbage collection
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2012, 14:38
  3. Garbage collection
    By Septi in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 14:13
  4. QString, QStringList and garbage collection...
    By PaladinKnight in forum Newbie
    Replies: 4
    Last Post: 5th April 2010, 16:22
  5. question about garbage collection
    By Dumbledore in forum Qt Programming
    Replies: 4
    Last Post: 18th December 2007, 22:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.