Java 垃圾回收規則

Rules available in this category:
  1. Do_not_remove_listeners_inside_finalize
  2. Avoid_using_synchronized_code_within_finalizers
  3. Avoid_static_collections
  4. Always_call_reset
  5. Provide_protected_access_to_finalizers
  6. Use_array_of_long_instead_of_Dates
  7. Do_not_call_finalize_directly
  8. Always_reuse_calls_to_java_awt_Graphics_getClipBounds_method
  9. Avoid_calling_java_lang_Runtime_runFinalization
  10. Avoid_explicitly_calling_gc
  11. Avoid_empty_finalizers
  12. Always_call_super_finalize
  13. Always_call_super_finalize_in_finally_block

Rule 1: Do_not_remove_listeners_inside_finalize
Severity:  High
Rule:  Avoid removing listeners inside finalize method.
Reason:  Avoid removing listeners inside finalize method.
Usage Example: 
package com.rule;

import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class Do_not_remove_listeners_inside_finalize_violation implements PropertyChangeListener
{
Component comp;

public Do_not_remove_listeners_inside_finalize_violation(Component c)
{
comp = c;
comp.addPropertyChangeListener(this);
}

protected void finalize() throws Throwable
{
comp.removePropertyChangeListener(this); // VIOLATION
super.finalize();
}

public void propertyChange(PropertyChangeEvent evt)
{
}
}
Should be written as:
Remove the listeners when they are no longer required, may be from a seperate method used for cleanup.

Rule 2: Avoid_using_synchronized_code_within_finalizers

Severity:  High
Rule:  Avoid using synchronized code within finalizers(finalize method).
Reason:  Avoid using synchronized code within finalizers(finalize method).
Usage Example: 
package com.rule;
public class Avoid_using_synchronized_code_within_finalizers_violation 
{
public void finalize()
{
syncMethod();             // Violation
}
public synchronized void syncMethod()
{
//...
}
}
Should be written as:
Avoid using synchronized code inside finalize method.

Rule 3: Avoid_static_collections

Severity:  High
Rule:  Disallows the use of static collection fields.
Reason:  Disallows the use of static collection fields.
Usage Example: 
public class Avoid_static_collections_violation
{
    public static Vector vector = new Vector (5); // VIOLATION

    public static void addToVector (Object o) 
    {
    vector.add(o);
    }

}
Should be written as:
public class Avoid_static_collections_correction 
{
    public Vector vector = new Vector (5); // CORRECTION


    public static void addToVector (Object o) 
    {
    vector.add(o);
    }

}
Reference:  Java Performance tunning by Jack Shirazi

Rule 4: Always_call_reset

Severity:  High
Rule:  If you are using ObjectStream, do remember to call reset() method as ObjectStream classes keep a reference to all objects written or read until the `reset()' method is called and hence the objects can not be garbage collected.
Reason:  If you are using ObjectStream, do remember to call reset() method as ObjectStream classes keep a reference to all objects written or read until the `reset()' method is called and hence the objects can not be garbage collected.
Usage Example: 
package com.rule;
import java.io.ObjectOutputStream;
import java.io.IOException;

class Always_call_reset_violation
{
public void writeToStream(ObjectOutputStream os, String s)throws IOException  
{
os.writeObject (s);// VIOLATION
}
}
Should be written as:
package com.rule;
import java.io.ObjectOutputStream;
import java.io.IOException;

class Always_call_reset_correction
{
   public void writeToStream(ObjectOutputStream os, String s)throws IOException
   {
    os.writeObject (s);
    os.reset();   // CORRECTION
   }
}

Rule 5: Provide_protected_access_to_finalizers

Severity:  High
Rule:  Provide protected access to finalize method.
Reason:  Provide protected access to finalize method.
Usage Example: 
package com.rule;

public class Provide_protected_access_to_finalizers_violation 
{
public void finalize() //Violation
{
}
}
Should be written as:
package com.rule;

public class Provide_protected_access_to_finalizers_corection 
{
protected void finalize() //Correction
{
}
}
Reference:  Reference not available.

Rule 6: Use_array_of_long_instead_of_Dates

Severity:  High
Rule:  Avoid using array of 'Date' objects, use an array of "long" instead.
Reason:  Avoid using array of 'Date' objects, use an array of "long" instead.
Usage Example: 
package com.rule;
import java.util.Date;
class Use_array_of_long_instead_of_Dates_violation
{
private final int size = 10;
private Date d[] = new Date[size]; // VIOLATION

public void method()
{
if(d != null)
{
d = null;
}
}

}
Should be written as:
package com.rule;
class Use_array_of_long_instead_of_Dates_correction
{
private final int size = 10;
private long d[] = new long[size]; // CORRECTION

public void method()
{
if(d != null)
{
d = null;
}
}
}
Reference:  Reference Not Available.

Rule 7: Do_not_call_finalize_directly

Severity:  Medium
Rule:  The finalize method is called by garbage collector and it should not be called directly
Reason:  The finalize method is called by garbage collector and it should not be called directly
Usage Example: 
package com.rule;

class Do_not_call_finalize_directly_violation
{
static void method(Do_not_call_finalize_directly_violation obj) throws Throwable
{
obj.finalize();
}

protected void finalize() throws Throwable
{
super.finalize();
}

}
Should be written as:
package com.rule;

class Do_not_call_finalize_directly_violation
{
static void method(Do_not_call_finalize_directly_violation obj) throws Throwable
{
//obj.finalize();
}

protected void finalize() throws Throwable
{
super.finalize();
}

}

Rule 8: Always_reuse_calls_to_java_awt_Graphics_getClipBounds_method

Severity:  Medium
Rule:  The getClipBounds method always returns a new rectangle, thereby allocating more memory on every call.
Reason:  The getClipBounds method always returns a new rectangle, thereby allocating more memory on every call.
Usage Example: 
import java.awt.Graphics;

public class MyClass
{

public void paint(Graphics g)  // VIOLATION
{
         int firstColLine = g.getClipBounds().x;
              int lastColLine = g.getClipBounds().x + g.getClipBounds().width;
     }

}
Should be written as:
import java.awt.Graphics;
import java.awt.Rectangle;

public class MyClass
{

public void paint(Graphics g)  // VIOLATION
{
Rectangle rec = g.getClipBounds(); // CORRECTION
int firstColLine = rec.x; // CORRECTION
int lastColLine = rec.x + rec.width; // CORRECTION
     }

}
Reference:  No references available.

Rule 9: Avoid_calling_java_lang_Runtime_runFinalization

Severity:  Medium
Rule:  One should not invoke garbage collection manually.
Reason:  One should not invoke garbage collection manually.
Usage Example: 
public class NoRunFinalization
{
public static void main( String[] args)  
{
for ( int i = 0; i < args.length; ++i ) 
{
System.out.println( args[ i ] );
}
Runtime.getRuntime().runFinalization();
}
}
Should be written as:
public class NoRunFinalization
{
public static void main( String[] args)  
{
for ( int i = 0; i < args.length; ++i ) 
{
System.out.println( args[ i ] );
}
}
}
Reference:  No references available.

Rule 10: Avoid_explicitly_calling_gc

Severity:  Medium
Rule:  Avoid explicitly calling System.gc().
Reason:  Avoid explicitly calling System.gc().
Usage Example: 
package com.rule;

class Avoid_explicitly_calling_gc
{
public void print()
{
Object[] oa = getObjects();
process(oa);
System.gc(); // Violation.
}
}
Should be written as:
 
 
Reference:  Reference Not Available.

Rule 11: Avoid_empty_finalizers

Severity:  High
Rule:  Avoid empty finalize method.
Reason:  Avoid empty finalize method.
Usage Example: 
package com.rule;

public class Avoid_empty_finalizers_violation
{
public void finalize() // Violation
{

}
}
Should be written as:
package com.rule;

public class Avoid_empty_finalizers_correction
{
public void finalize() // Correction
{
try
{
super.finalize();
}
catch (Throwable e)
{
// ...
}
}
}
Reference:  Reference not available.

Rule 12: Always_call_super_finalize

Severity:  High
Rule:  If you are over writing finalize method then make sure that it calls super.finalize() method.
Reason:  If you are over writing finalize method then make sure that it calls super.finalize() method.
Usage Example: 
package com.rule;

public class Always_call_super_finalize_violation
{
protected void finalize () throws Throwable  // VIOLATION
{  
//...
}
}
Should be written as:
package com.rule;

class Always_call_super_finalize_correction
{
protected void finalize () throws Throwable
{
   super.finalize();  // CORRECTION
}
}

Rule 13: Always_call_super_finalize_in_finally_block

Severity:  High
Rule:  If there is a try block in the finalize method, super.finalize() should be called from the finally block.
Reason:  If there is a try block in the finalize method, super.finalize() should be called from the finally block.
Usage Example: 
public class AlwaysCall_super_finalize_inFinallyBlock 
{
    protected void finalize () throws Throwable 
    {
       try 
       {
       } 
       catch (Exception e) 
       {
       } 
       finally // VIOLATION, 'super.finalize()' is not invoked
       {  
           return;
       }
    }


    public class AlwaysCall_super_finalize_inFinallyBlockMember 
    {
protected void finalize() 
{
     try 
     {
            close();
    super.finalize();
} catch (Exception e) 

//VIOLATION, missing a "finally" block that invokes 'super.finalize()'
        //Note that if 'close()' throws an exception, 'super.finalize()' would not be invoked
     }
    }
}
Should be written as:
public class AlwaysCall_super_finalize_inFinallyBlock 
{
    protected void finalize () throws Throwable 
    {
       try 
       {
       } 
       catch (Exception e) 
       {
       } 
       finally // VIOLATION, 'super.finalize()' is not invoked
       {  
           super.finalize(); // CORRECTION
       }
    }


    public class AlwaysCall_super_finalize_inFinallyBlockMember 
    {
protected void finalize() 
{
     try 
     {
            close();
} catch (Exception e) 

     }
     finally
     {
         super.finalize(); // CORRECTION
     }
         
    }
}
Reference:  "The Java Programming Language, Second Edition", by Ken Arnold and James Gosling. Page 49.  

原文引自:http://www.appperfect.com/support/java-coding-rules/garbagecollection.html
相關文章
相關標籤/搜索