001/****************************************************************************
002 * Copyright/Copyleft:
003 *
004 * For this source the LGPL Lesser General Public License,
005 * published by the Free Software Foundation is valid.
006 * It means:
007 * 1) You can use this source without any restriction for any desired purpose.
008 * 2) You can redistribute copies of this source to everybody.
009 * 3) Every user of this source, also the user of redistribute copies
010 *    with or without payment, must accept this license for further using.
011 * 4) But the LPGL is not appropriate for a whole software product,
012 *    if this source is only a part of them. It means, the user
013 *    must publish this part of source,
014 *    but don't need to publish the whole source of the own product.
015 * 5) You can study and modify (improve) this source
016 *    for own using or for redistribution, but you have to license the
017 *    modified sources likewise under this LGPL Lesser General Public License.
018 *    You mustn't delete this Copyright/Copyleft inscription in this source file.
019 *
020 * @author Hartmut Schorrig: hartmut.schorrig@vishia.de, www.vishia.org
021 * @version 0.93 2011-01-05  (year-month-day)
022 *******************************************************************************/ 
023package org.vishia.java2C.test;
024
025public class TestgarbageCollector
026{
027
028  /**This static reference references a new created instance, so it isn't delete from the garbage collector. */
029  private static SimpleClass staticData; 
030  
031  
032  final private void useCreatedInstanceInternal()
033  {
034        /**Creates a new instance, works with it, but after them the instance isn't need furthermore. 
035     * It will be deleted by garbage collector. */
036    { SimpleClass dataTemp = new SimpleClass();
037      dataTemp.x1 = 34;
038      /**To concat a temporary variable will be used and freed from GC. */
039      System.out.println("test:" + dataTemp.x1);
040    }  
041    
042  }
043  
044  /**This class creates a new instance and returns the pointer to them. It should not be activated
045   * for handling with garbage collection.
046   * @return the instance reference.
047   * @java2c=return-new.
048   */
049  private SimpleClass createAndReturnInstance()
050  {
051    SimpleClass obj = new SimpleClass();
052    /**Because this obj is returned, it won't be activated for garbage collection. */
053    return obj;
054  }
055  
056  
057  /**This method calls the method {@link #createAndReturnInstance()} to create an instance, which is not stored
058   * in the class. The reference is known only in a stack variable. The reference is used. 
059   * The garbage collector should not attack this instance. To force working of garbage collection,
060   * a Thread.sleep() is built in this method. 
061   */
062  public void testNewInstance()
063  {
064    /**Because the data are returned from an method, it will be activated for garbage collection 
065     * at end of this method, except if the reference is returned.
066     */
067    SimpleClass data = createAndReturnInstance();
068    for(int ii =0; ii<5; ii++){
069      try{ Thread.sleep(1000);} 
070      catch (InterruptedException e){}
071      data.x1 += 234;
072      System.out.println("testNewInstance: " + data.x1);
073    }  
074  }
075  
076  
077  
078  
079  
080  void test()
081  {
082    /**Creates a new instance and referes it to a static reference: */
083    { SimpleClass data = new SimpleClass();
084      staticData = data;
085    }
086    System.gc();
087    
088    useCreatedInstanceInternal();
089    
090    System.gc();
091    
092  }
093  
094  
095  
096}