当前位置:网站首页>对象在内存中存在形式&内存分配机制
对象在内存中存在形式&内存分配机制
2022-07-27 19:07:00 【小唐学抓娃】
一、对象在内存中存在形式
1、首先,内存分为三部分:栈,堆,方法区。
- 栈:用来存放对象的引用以及方法在此运行。
注意:
- 方法运行时会进入栈,方法结束后立即出栈。
- 方法出栈后,在该方法中的对象变量都会被回收。
- 堆 :用来存放一切用new 创建的对象。(对象的地址,对象的属性成员变量等)
方法区:用来存放类的相关信息(属性、方法),以及常量池。
常量池中只存放字符串类型的常数。(比如在堆中,属性类型是字符串类型的,不直接存放值,而是存放地址,这个地址就是常量池里对应的值的地址。)
- 关于常量池是存在于堆中还是方法区中的看法。
JDK1.8中字符串常量池和运行时常量池逻辑上属于方法区,但是实际存放在堆内存中,因此既可以说两者存放在堆中,也可以说两则存在于方法区中,这就是容易造成误解的地方。
二、类和对象的内存分配机制
先看下面一段代码
Person p1 = new Person();
p1.age = 10;
p1.name = "小明";
Person p2 = p1;//把p1赋给了p2 让p2指向p1
System.out.println(p2.age);
请问:p2.age究竟是多少?并画出内存图(假设一个地址)
Java 内存的结构分析
1) 栈: 一般存放基本数据类型(局部变量)
2) 堆: 存放对象(Cat cat , 数组等)
3) 方法区:常量池(常量,比如字符串), 类加载信息
边栏推荐
- Software test interview questions: the steps to write test cases by drawing cause and effect diagrams are___ And transforming the cause and effect diagram into a state diagram in five steps. What are
- Worthington毒液中核酸外切酶的特征及相关文献
- xml编写补间动画 PopupWindow实现出现退出的动画
- How to realize a good knowledge management system?
- 数组扩容、排序、嵌套语句应用
- 简单手动实现Map
- Traps and limitations of Engineering Technology Development
- ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
- CocoaPods 重装
- 微软商店无法下载应用,VS2019无法下载插件问题解决方案
猜你喜欢

零钱通项目(两个版本)含思路详解

day 1 - day 4

Puzzle (021) eliminate problems

Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase

IDEA常用快捷键及设置方法

高并发遇到死锁了,如何搞?

"Geography language" large model Wenxin Ernie geol and its application

Some operations about Anaconda (installing software and quickly opening)

@Detailed introduction of requestparam annotation

MySQL执行过程及执行顺序
随机推荐
Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design
单核CPU, 1G内存,也能做JVM调优吗?
ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
怎么还有人问 MySQL 是如何归档数据的呢?
软件测试面试题:什么是回归测试?
Software testing interview question: what project documents need to be referred to in designing the system test plan?
ECCV 2022 | China University of science and Technology & jd.com proposed: data efficient transformer target detector
Worthington蘑菇多酚氧化酶的特性及测定方案
Software testing interview question: what is regression testing?
Oppo core making plan officially announced: the first chip or oppo M1
Guava Cache 原理分析与最佳实践
IDEA常用快捷键及设置方法
软件测试面试题:软件验收测试包括正式验收测试、alpha测试、beta测试三种测试?
How to realize a good knowledge management system?
@Detailed introduction of requestparam annotation
LinkedList underlying source code
看起来是线程池的BUG,但是我认为是源码设计不合理。
除了「加机器」,其实你的微服务还能这样优化
ZABBIX monitoring service (III) configuration management graphics and windows