什么是静态变量的实际内存空间? [英] What is the actual memory place for static variables?

查看:89
本文介绍了什么是静态变量的实际内存空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态变量分配给整个程序执行期间,所以堆
和heap都不便于使用。那它在哪里?应该有一些地方加载?

解决方案

当一个类被加载时,静态字段被初始化,并且当该类的类加载器被卸载时,被丢弃。它们可以被清理,甚至可以在另一个类加载器中复制。



对于那些使用OSGi的应用程序,静态变量不会在应用程序的整个生命周期中生存重新加载多次。

实现的方式可能与JVM有关,但Sun / Oracle JVM创建一个对象来存放类的静态字段。这个对象可以通过Unsafe类访问,它也可以用来检查这个objects字段。


Static variable is allocated for the entire duration of program's execution, so neither stack nor heap are convenient for it. Then where is it? There should be some place where it is loaded ?

解决方案

Static fields are initialised when a class is loaded and and are discarded when the classloader for that class is unloaded. They can be cleaned up, even duplicated in another class loader.

For applications like those this use OSGi, static variables don't live for the life of the application can be reloaded many times.

How this is implement may be JVM dependant but the Sun/Oracle JVM creates an "object" to hold the static fields for a class. This object is accessible via the Unsafe class which can also be used to examine this "objects" fields.

这篇关于什么是静态变量的实际内存空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆