This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Can anyone please let me know, is it advisable to declare the logger variable as static?
In all my previous concerns, I have done the same thing. Here while I analyzing the memory files, this variable took more memory and some of the developers advised me not to use Static keyword here.
private static final Logger logger = Logger.getLogger(myClass.class);
Actually i got some bugs which related to the application performance.
We are using IBM WAS 7.0 as application server, EJB 3.0 for Business logic and SWT as the front end.
While performing some actions in the application, we have monitored that java.exe of the application server goes high and its not getting reduced while that action has been done. And one certain time, this results Heapdump file generation on the server profile.
We have analyzed the heap dump files and it says the "OutOfMemory" Exception and much memory took for this STATIC Logger instantiation.
Some of the developers gave suggestion that to remove the STATIC declaration for the logger.
for eg. public Logger logger = Logger.getLogger(myClass.class);
Joined: Jan 16, 2009
Moreover, we have implemented this Log for some of the business logic classes to enable the logging functinality.
@Bala: is your class (where ou have added the logger) is singlton ?
if YES, we CAN add Logger as instance variable
if NO then keep it static else there would be performance degradation ....
Also check the memoery foot print keeping loggers as static / instance.
Since LIFE (SCOPE ) of the STATIC variable would be different than the INSTANCE varaible; you MAY see some difference !
May I ask you few questions?
Have you ACTUALLY seen any performance improvement after defining loggers as INSTANCE fields?
Which Log api are you using? (hopefully Apache )
what is the appender size you have configured
is Appender on local drive or on network?
“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
"We have analyzed the heap dump files and it says the "OutOfMemory" Exception and much memory took for this STATIC Logger instantiation."
how did you find that the static logger was taking most of the memory?
This seems highly unlikely unless you start your application will really low parameters for the PermGen space. Class objects and class variables (i.e. static members) are stored in the PermGen area of the heap. This area is not subject to garbage collection. So if it is truly the static logger that is causing an OutOfMemoryError, then the only explanation is that you are allocating very little memory for the PermGen during your server startup.