title: Object reinitialization : memory allocation
body: <p>Was Learning Multithread Socket programming in java :</p>
  
  <p>Came across this piece of code :</p>
  
  <pre><code>ServerSocket server=new ServerSocket(8888);
  
  while(true){
          counter++;
          Socket serverClient=server.accept();
          Threads t = new Threads(serverclient)
          }
  </code></pre>
  
  <p>Here For every connection we are creating an Object of Threads(class which has Implemented Runnable Interface).</p>
  
  <p>My query is :
  We are not creating a new object but re initializing the same object with new value.
  So after re initializing new object reference the old object reference should get deleted</p>
  
  <p>So Created this code to understand the constructor working in java.</p>
  
  <pre><code>public class free_memory
  {
  
      public static void main(String args [])
      {   int i;
  
          long UsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
          System.out.println("Total memory used before execution : " + UsedMem / 1024);
  
          new temp();
  
          UsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
          System.out.println("Total memory used after  execution of 1 constructor: " + UsedMem/ 1024);
  
          for(i=0;i&lt;10;i++)
          {
          new temp();
          }//for
  
          UsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
          System.out.println("Total memory used after  execution of 10 constructor: " + UsedMem/ 1024);
  
  
          for(i=0;i&lt;10;i++)
          {
          temp a = new temp();
          }//for
  
          UsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
          System.out.println("Total memory used after  execution of 10 constructor: " + UsedMem/ 1024);
  
          int a;
  
          UsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
          System.out.println("Allocating an integer: " + UsedMem/ 1024);
      }//main
  }
  
  
  
  class temp
  {
      int a[] = new int[10];
      int i;
  
      temp()
      {
          for(i=0;i&lt;10;i++)
          {
              a[i] = 10;
          }//for
  
      }//constructor
  
  
  }//class
  </code></pre>
  
  <p>Output is : </p>
  
  <pre><code>Total memory used before execution : 1599
  Total memory used after  execution of 1 constructor: 2048
  Total memory used after  execution of 10 constructor: 2048
  Total memory used after  execution of 10 constructor: 2048
  Allocating an integer: 2048
  </code></pre>
  
  <p>This code is not allocating any extra memory.</p>
  
  <p>Could you please help 
  We should create an array of objects </p>
  
  <p>// my suggestion it should be like this :</p>
  
  <pre><code>Socket serverClient=server.accept();
              Threads t[i] = new Threads(serverclient)
  </code></pre>
  
  <p>//original code</p>
  
  <pre><code>Socket serverClient=server.accept();
              Threads t = new Threads(serverclient)
  </code></pre>
  
  <p>Thanks in advance</p>