简体   繁体   中英

Java memory model for static methods

I come from operating systems and background of C where the world is simple when code is compiled. Need to deal and understand stack, heap text section etc.

When I started learning Java(I do know about JVM and garbage collector), I got amused by static methods. As per my understanding all the instances of a class do get created in the heap and then do get cleaned. However, for a static method, you don't need an instance of the class.

So, can some one please explain how non static methods and static methods differ in memory model. Do both of them reside in the text section of the memory. Or I am messing up things completely.

Thanks

In Java, the bytecode for the classes (and that includes their methods, both static and instance) is part of the heap (usually in a special "permanent generation" section for long-lived objects).

Classes can be garbage-collected, too, but this usually does not happen much (only when the class was loaded from a non-system classloader and that whole classloader becomes obsolete, for example when a web application is unloaded).

However, for a static method, you don't need an instance of the class.

Right. But all methods are part of the class definition and loaded together when the class is loaded. Even if you never make an instance of a class, the code for all instance methods will be loaded into heap memory.


And then there is JIT compilation to native code: With Hotspot, the bytecode for frequently used methods is compiled further into native machine code. The result of that does go somewhere outside of the heap into native memory, and that only happens for methods that are actually being used (static or not).

Your understanding that all instances of a class get created in the heap...

Not exactly correct, all classes get compiled into object byte code, otherwise the JVM has nothing to execute. Instance and static methods all generate the same object byte code. Even non-static classes only generate a single version of their object byte code. All instance classes have their own pointer to where their execution is in that single copy of byte code. The real difference is in the data members of the class. Each instance of a non-static class has to have its own copy of all the non-static data members (variables), but static data members only have a single copy in memory since static data members of a class are shared by all instances of that class static or non-static.

A static class or the static data members of a non-static class all have a single copy of themselves in memory.

A non-static class still has just a single copy of its object code in memory, it is only the non-static data that gets a copy in memory for each instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM