Sun Microsystems 820434310 Server User Manual


 
String str = "testing";
str = str + "abc";
The compiler translates this code as:
String str = "testing";
StringBuffer tmp = new StringBuffer(str);
tmp.append("abc");
str = tmp.toString();
Therefore, copying is inherently expensive and overusing it can reduce performance
signicantly.
Assign null to VariablesThat Are No Longer Needed
Explicitly assigning a null value to variables that are no longer needed helps the garbage
collector to identify the parts of memory that can be safely reclaimed. Although Java provides
memory management, it does not prevent memory leaks or using excessive amounts of
memory.
An application may induce memory leaks by not releasing object references. Doing so prevents
the Java garbage collector from reclaiming those objects, and results in increasing amounts of
memory being used. Explicitly nullifying references to variables after their use allows the
garbage collector to reclaim memory.
One way to detect memory leaks is to employ proling tools and take memory snapshots after
each transaction. A leak-free application in steady state will show a steady active heap memory
after garbage collections.
Declare Methods as nal Only If Necessary
Modern optimizing dynamic compilers can perform inlining and other inter-procedural
optimizations, even if Java methods are not declared final. Use the keyword final as it was
originally intended: for program architecture reasons and maintainability.
Only if you are absolutely certain that a method must not be overridden, use the final
keyword.
Declare Constants as static nal
The dynamic compiler can perform some constant folding optimizations easily, when you
declare constants as static final variables.
Avoid Finalizers
Adding nalizers to code makes the garbage collector more expensive and unpredictable. The
virtual machine does not guarantee the time at which nalizers are run. Finalizers may not
always be executed, before the program exits. Releasing critical resources in finalize()
methods may lead to unpredictable application behavior.
JavaProgrammingGuidelines
SunGlassFishEnterpriseServer2.1PerformanceTuningGuide January200928