10 Replies Latest reply: Mar 7, 2013 6:09 PM by EJP RSS

    De-Allocating Memory

    995455
      Hello!

      i'm a programmer with extensive programming knowledge about C++ and hence i know about memory management....
      I have made this small little program in Java:

      -----

      Class myThread extends Thread{

      public void run(){

      System.out.println("I RAN!!!");
      new myThread().start();

      }

      }

      Class mainClass{

      public static void main(String args[]){

      new myThread().start();

      }

      }

      -----

      but the thing is, that if i run this program for a long time, it says something like memory couldn't be allocated or something like that.
      all i wanna do is delete the current thread before starting the new one...

      is is possible?

      please help....
        • 1. Re: De-Allocating Memory
          r035198x
          Your thread's run method starts a new thread whose run method starts a new thread whose run method starts a new thread ...
          • 2. Re: De-Allocating Memory
            Kayaman
            992452 wrote:
            i'm a programmer with extensive programming knowledge about C++ and hence i know about memory management....
            Okay. Well, as you know, there's no direct memory management in Java.
            I have made this small little program in Java:
            Why on earth?
            but the thing is, that if i run this program for a long time, it says something like memory couldn't be allocated or something like that.
            What's "long time"? And what OS are you on? And what's the exact error message?
            all i wanna do is delete the current thread before starting the new one...
            Well, you can't delete threads. Even though there's the Thread.stop() method, it's been deprecated.
            What are you really trying to do? This whole mindless spawning of threads can't be what you're after (and it would be ridiculously stupid to just create new threads instead of pooling them).
            • 3. Re: De-Allocating Memory
              TPD-Opitz
              What do you wanted to proove with this progam? That Java thread creation is fast enough to fill the whole memory before the GC is able to remove the dead ones?

              Did you write the same in C++?
              did it fail there?

              bye
              TPD
              • 4. Re: De-Allocating Memory
                939520
                I suggest you read a book on Java and avoid trying to port any of your knowledge of C++ to Java. Its the wrong approach.
                • 5. Re: De-Allocating Memory
                  EJP
                  all i wanna do is delete the current thread before starting the new one...
                  Then you'll have to write different code. Your present code requires all threads, i.e. an infinite number of them, to be in existence before any of them can exit.
                  • 6. Re: De-Allocating Memory
                    gimbal2
                    This looks like the work of a programmer who likes recursive code a little too much. Pretty neat way to go awry to be honest - recursive thread spawning! Now there is a way to keep the factory pipes smoking.

                    Yet another thread that proves that using pre-existing knowledge of another language and architecture to try and reason out Java can only lead to failure.
                    • 7. Re: De-Allocating Memory
                      EJP
                      I don't see that that's really true here. The same kind of code would have the same problems in C++ as well. Rubbish code is rubbish in any programming language.
                      • 8. Re: De-Allocating Memory
                        baftos
                        I would like to see the equivalent C++ code that does not misbehave. I am not being sarcastic or anything, just curious.
                        • 9. Re: De-Allocating Memory
                          gimbal2
                          baftos wrote:
                          I would like to see the equivalent C++ code that does not misbehave. I am not being sarcastic or anything, just curious.
                          Would be something. Although I really can't be @rsed to go and install a C++ development environment to compile and try it out :/ I'm terrible like that.
                          • 10. Re: De-Allocating Memory
                            EJP
                            I would like to see the equivalent C++ code that does not misbehave. I am not being sarcastic or anything, just curious.
                            Me too. I'd love to see it. I don't believe it exists.