静态类变量存储在内存中? [英] Where are static class variables stored in memory?

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

问题描述

这是如何存储静态数组的后续问题在Java内存中?。因此C / C ++中的全局变量存储在内存的静态数据段中。

This is a follow-up question to How are static arrays stored in Java memory? .

但是在Java / C ++中的静态类变量呢?

So global variables in C/C++ are stored in the static data segment of memory. But what about static class variables in Java/C++?

它不能是静态数据段,因为你不知道什么/在整个程序的持续时间(因为反射)。这绝对不是堆栈,因为这没有任何意义。

It can't be the static data segment because you don't know what/how many classes are going to be referenced throughout the duration of your program (because of reflection). It's definitely not the stack because that makes no sense. Storing it on the heap is also kind of iffy.

推荐答案

在Java中,在一个低级别,类静态变量确实存储在堆上,以及所有其他类元数据。对于Java,它们看起来像全局变量,但是对于JVM的低级堆管理例程,它们是动态数据(尽管为了提高GC效率可能会稍微处理它们,因为它们可能很长)。

In Java, at a low level, class static variables are indeed stored on the heap, along with all other class metadata. To Java, they look like globals, but to the JVM's low level heap management routines, they're dynamic data (although they may be treated slightly specially in order to improve GC efficiency, since they're likely to be long lived). After all, classes can be unloaded by unreferencing their classloader.

至于它是否与C malloc(),不太可能。大多数JVM控制他们的堆在低水平;他们从操作系统获取一块内存,并自己分开。因此,大多数Java数据(包括静态数据)不存储在malloc堆中,而是存储在由JVM管理的单独堆中。

As for whether it's the same as the C malloc(), not likely. Most JVMs take control of their heaps at a low level; they grab a chunk of memory from the OS and divvy it up themselves. As such, most Java data, including static data, are not stored in the malloc heap, but rather in a separate heap managed by the JVM.

这篇关于静态类变量存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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