java - How JVM manages memory for methods?
问题描述
For example we have an instance of MyClass
and it contains 1 method. This method should be kept in memory. When this instance of MyClass
is being GC'ed, is a reference to this method being deleted as well? I want to figure out weather doing everything via Dependency Injection (hence creating new instances of every class) requires less memory and more efficient or simple Helper
classes with a bunch of static methods are still good.
解决方案
The instance methods of an object are stored in its class object (only one copy should exist), they are not "copied" with each new instance, instead, under the hood each instance holds a reference to the method implementation residing in the class object. Instances are garbage collected not the class data. The class data gets stored in permgen space or metaspace depending on the java version. Garbage collectors work specifically on the heap where the instances are created not on the permgen or metaspace.
推荐阅读
- azure - 如何在规模集 VM 之间有效共享数据
- android - Firebase Auth EmailAndPassword 和实时数据库错误
- entity-framework - 无法使用 GitLab CI 脚本中的 EF Core 连接到 AWS 数据库
- python - 矢量化张量流步骤的正确方法
- javascript - Font Awesome 5,如何设置 svg 伪元素的样式?
- wso2 - wso2 esb如何连接和查询到HBASE
- sql - 根据另一个 SQL 中最近的日期返回表中的值
- vba - 使用 VBA 引用另一个工作表中的单元格,该工作表本身由其文本引用
- android - 如果速度为 0,Location.hasSpeed() 是否返回 true 或 false?
- java - 谷歌控制台警告:您的应用程序启动时间超过 20 秒