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

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

问题描述

在程序执行的整个持续时间内分配一个静态变量,因此堆栈堆也不方便.那么那个变量在哪里呢?不是应该有什么地方加载的吗?

A static variable is allocated for the entire duration of a program's execution, so neither stack nor heap are convenient for it. Then where is that variable? Shouldn't there be some place where it is loaded from?

推荐答案

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

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

对于像使用 OSGi 的应用程序,静态变量不会在应用程序的整个生命周期中都存在.它们可以重新加载多次.

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

这是如何实现的可能取决于 JVM,但 Sun/Oracle JVM 创建了一个对象"保存类的静态字段.该对象可通过 Unsafe 类访问,该类也可用于检查该对象".字段.

How this is implement may be JVM dependent, 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天全站免登陆