Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods loading/unloading and memory area in java?

I have four questions regarding static methods in java:

1) When static methods are loaded in memory? when class loaded or when method get called first time?

2) When static method is unloaded from memory? When class unloaded or when execution flow of that method is ended after call?

3) In which memory area of JVM static methods are loaded? In stack memory area or there is different kind of memory available in JVM for static methods(I have read about some static memory also)?

4) Is it good to have so many static methods in our application?

I have gone through so many questions over this site and over other sites, but they don't clearly specify it.

like image 635
Gaurav Jeswani Avatar asked Sep 12 '15 12:09

Gaurav Jeswani


3 Answers

1) When static methods are loaded in memory? when class loaded or when method get called called first time?

When that particular class loaded by the class loader.

2) When static method is unloaded from memory? When class unloaded or when execution flow of that method is ended after call?

When JVM garbage collected that particular class loader which loaded the static class.

3) In which memory area of JVM static methods are loaded? In stack memory area or there is different kind of memory available in JVM for static methods(I have read about some static memory also)?

Typically in Pergemen space.

where is a static method and a static variable stored in java. In heap or in stack memory

4) Is it good to have so many static methods in our application?

Yes, when they are solving the purpose individually.

like image 74
Suresh Atta Avatar answered Oct 07 '22 13:10

Suresh Atta


  • A method is just bytecode so it is loaded when the class gets loaded, but this is not specific to static methods, a member method would be loaded at the same time
  • When the class is unloaded. But this is very rare : Unloading classes in java?
  • Classes are stored in the heap, in the PermGen space (not GCed)
  • It depends. If it is for syntactic sugar or factory methods that's fine. If it is for implementing functionalities which could be achieved by a stateless and serializable object, that's most probably fine too. If it is to gain access to some attributes or logic from places of the code which should not be able to do it, that's wrong.
like image 38
Dici Avatar answered Oct 07 '22 13:10

Dici


1) When static methods are loaded in memory? when class loaded or when method get called called first time?

ans) when class loaded

2) When static method is unloaded from memory? When class unloaded or when execution flow of that method is ended after call?

ans) Static methods gets loaded when class is loaded by ClassLoader, and would be removed when it is Unloaded

3)In which memory area of JVM static methods are loaded? In stack memory area or there is different kind of memory available in JVM for static methods(I have read about some static memory also)?

ans)Inside a Java virtual machine instance, information about loaded types is stored in a logical area of memory called the method area. When the Java virtual machine loads a type, it uses a class loader to locate the appropriate class file. The class loader reads in the class file--a linear stream of binary data--and passes it to the virtual machine. The virtual machine extracts information about the type from the binary data and stores the information in the method area. Memory for class (static) variables declared in the class is also taken from the method area.

4)Is it good to have so many static methods in our application?

ans)One rule-of-thumb: ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static.

like image 23
Nik Patel Avatar answered Oct 07 '22 12:10

Nik Patel