简体   繁体   English

静态方法的Java内存模型

[英]Java memory model for static methods

I come from operating systems and background of C where the world is simple when code is compiled. 我来自C的操作系统和背景,那里的代码编译时世界很简单。 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. 当我开始学习Java(我确实了解JVM和垃圾收集器)时,我对静态方法感到很开心。 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). 在Java中,类的字节码(包括它们的方法,包括静态方法和实例方法)是堆的一部分(通常在长寿命对象的特殊“永久生成”部分中)。

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). 类也可以被垃圾回收,但是这种情况通常不会发生太多(仅当从非系统类加载器加载该类并且整个类加载器变得过时(例如,当卸载Web应用程序时))。

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. 然后,将JIT编译为本地代码:使用Hotspot,将常用方法的字节码进一步编译为本地机器代码。 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. 并非完全正确,所有类都被编译为目标字节代码,否则JVM没有任何执行。 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. 非静态类在内存中仍然只有其目标代码的一个副本,只有非静态数据为每个实例在内存中获取一个副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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