wood burning stoves 2.0*
The moose likes Performance and the fly likes Instantiates Logger as STATIC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Instantiates Logger as STATIC" Watch "Instantiates Logger as STATIC" New topic

Instantiates Logger as STATIC

Balakrishnan Nagarajan

Joined: Jan 16, 2009
Posts: 20

Hi Guyz,

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);

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

What are they suggesting, a Logger instance per object instance? Would that not be far, far worse?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Balakrishnan Nagarajan

Joined: Jan 16, 2009
Posts: 20
Hi Paul,

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);

Balakrishnan Nagarajan

Joined: Jan 16, 2009
Posts: 20
Moreover, we have implemented this Log for some of the business logic classes to enable the logging functinality.
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
@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'.”
Bhay Zone

Joined: Jul 13, 2009
Posts: 7
"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.

See if this link helps

It is sorta covered in the JavaRanch Style Guide.
subject: Instantiates Logger as STATIC
Similar Threads
Understanding thread interference
Naming logger instances
EJB Passivation Question
when we should go for a singleton class in java?
final instance variable