当前位置:网站首页>对象在内存中存在形式&内存分配机制
对象在内存中存在形式&内存分配机制
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) 方法区:常量池(常量,比如字符串), 类加载信息
边栏推荐
- Daily news on July 15, 2022: meta announced the launch of make-a-scene: AI image generation can be controlled based on text and sketches
- Will the United States prohibit all Chinese enterprises from purchasing American chips? Trump responded like this
- 除了「加机器」,其实你的微服务还能这样优化
- 面向3nm及以下工艺,ASML新一代EUV光刻机曝光
- Postgresql源码(65)新快照体系Globalvis工作原理分析
- A new technical director asked me to do an IP territorial function~
- Analysis of STL source code
- 自定义recycleView的删除&移动的动画
- The new CTO strongly prohibits the use of calendar?
- 简单手动实现Map
猜你喜欢

Characteristics of exonuclease in Worthington venom and related literature

Worthington磷脂酶A2研究丨磷脂酰胆碱2-乙酰水解酶

The solution that the laptop can connect to WiFi but the browser cannot open the web page

如何实现一个好的知识管理系统?

异常-Exception

Design of noise reduction link based on DSP

Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase

除了「加机器」,其实你的微服务还能这样优化

30 minutes to thoroughly understand the synchronized lock upgrade process

Technical practice behind bloom model: how to refine 176billion parameter model?
随机推荐
枚举和注解
Multi person collaborative development specification
PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
LinkedList underlying source code
Box model and element positioning
Search, insert and delete of hash table
软件测试面试题:假设有一个文本框要求输入10个字符的邮政编码,对于该文本框应该怎样划分等价类?
How to realize a good knowledge management system?
软件测试面试题:软件验收测试包括正式验收测试、alpha测试、beta测试三种测试?
【2022牛客多校第二场】K-Link with Bracket Sequence I
Unit-- read Excel
深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
Who is the sanctity of the six Chinese enterprises newly sanctioned by the United States?
Software testing interview question: how many strategies are there for system testing?
腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?
软件测试面试题:请说出这些测试最好由那些人员完成,测试的是什么?
说明书丨Worthington逆转录酶、重组 HIV 检测方案
Technology Management - we must focus on the big and let go of the small
简单手动实现Map
单核CPU, 1G内存,也能做JVM调优吗?