Basically i wanted to know why we use Static Methods in Java.
Because static methods are simpler than instance methods.
so my question is to save memory (from object creation ) we use static Methods ??
No. You should code with purpose and only create things you actually need. You create an instance when you need to, and don't create one when you don't. This is not for performance reasons but to reduce the conceptual weight of your code. If you have to maintain your code, or some elses code, you need to find why is something being done and it takes alot longer to determine that pointless code really is pointless. i.e. It takes longer to look for something which is not there, than something which is.
To support multi-threaded memory allocation, each thread has a thread local allocation buffer or TLAB.
The free space only shows you how much is free in the common pool, but doesn't could the size available in each thread's TLAB. If you allocate enough strings, you will see a sudden jump in memory usage as it loads another block.
What you can do is turn this off with -XX:-UseTLAB
// -XX:+UseTLAB heapUsedBefore: 5,368,848 heapUsedAfter: 5,368,848
// -XX:-UseTLAB heapUsedBefore: 535,048 heapUsedAfter: 535,096
public class One {
public static void main(String... args) {
Runtime runtime = Runtime.getRuntime();
long heapUsedBefore = runtime.totalMemory() - runtime.freeMemory();
String str = new String();
long heapUsedAfter = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("heapUsedBefore: %,d heapUsedAfter: %,d%n",
heapUsedBefore, heapUsedAfter);
}
}