java - Does all values which a static object went through take up space in memory while class is loaded?
问题描述
I know that static variables are stored in a specific place in memory. Whenever you declare an object as static, that object will never be recycled by gc as long as that class is loaded by the classloader .. so far so good.
But imagine that the variable that I defined as static has an internally non-static list, so even though the object itself will never be recycled, will that list be recycled normally? And even if I assign a new instance to a static object, will the old value not be recycled? Would all the values that this object went through take up space in memory?
解决方案
垃圾收集器从 开始扫描gc roots
,其中一个根是static
字段。
因此 aGC
将遍历从该根“开始”的所有条目,例如:
SomeStaticInstance
|
\ /
NonStaticList
如果NonStaticList
是可达marking
的(gc 阶段的阶段负责这个),它被认为是活着的,因此不符合GC
. 如果无法访问,则有资格收集。请注意,如果它不可访问,mark phase
甚至不会遍历任何可能具有来自 NonStaticList
的引用:
NonStaticList
|
\ /
RefHere
所以RefHere
不会被扫描。如果它有来自其他地方的引用,而不是来自NonStaticList
.
推荐阅读
- palantir-foundry - 如何在不使用 tranform 或 tranform_df 的情况下在 palantir Foundry 中导入单个数据集
- reactjs - react-firebaseui StyledFirebaseAuth 没有被编译
- visual-studio-code - 在vscode中如何向状态栏添加多个选项
- php - 如何在laravel中获取总和并分离每个ID的总数
- json - 谷歌识别的 JSON LD 格式的 Schema.org,但在网站控制台中显示错误
- compiler-errors - 为什么我的代码在混音时显示编译错误?
- sequelize.js - 为什么 Sequelize 为每个查询创建新连接?
- python - 将xml写入其中时如何删除数据框中的新行?
- websocket - WebSocket 连接到“wss://url”失败:未知原因
- java - 无重复的Java序列生成