Java Memory Management
How Memory works in Java
The role of the stack
- Each time you call a function, Java pushed the local variables for that function into the stack.
- When the method returns, all the data created on the stack for the method is popped or pulled from the stack.
- Whenever you reach a closing curly bracket, any local variables you declared inside the block that you're leaving is popped off the stack.
- Each thread has its own stack, so data on the stack can only be seen by the thread that owns the stack.
The role of the Heap
- All objects are stored on Heap, and shared across all threads.
- For the objects stored on the Heat there will be a pointer to the objects which is the variable reference stored on the stack.
Introduction to Garbage Collection
- String Pools
- Garbage Eligibility
Any objects on the Heap which cannot be reached through reference from the stack is eligible for GC.
- The gc() and finalize() methods
The gc() method suggests the JVM run GC process, but not guarantee that the JVM will do.
The finalize() method for the java.lang.object, this method is actually called by the gc. The gc might not run, so cannot rely on the finalize method being called.
Generational Garbage Collection
Mark and Sweep
- The general algorithm that GC use is called mark and sweep. It's a two stage process, the first stage is marking, and the second stage is sweeping.
- In the marking stage, the program's execution is first paused (stop the world event). It simply looks every variable in the stack and follows its reference, the object that it finds at the end of the reference is marked as being alive.
- In the sweeping stage, a full scan of the Heap take place, the memory occupied by the objects not marked of in use, can then be freed up. And finally, the objects are being kept are moved to a single continuous block of memory.
Using the Visual GC tool to view a soft leak
- Young generation (monor GC) VS. old generation (major GC)
- Can view these generations by the visual VM tool
a. $cd $JAVA_HOME/bin
b. $open jvisualvm
c. Need to install a plugin, go to the tools menu -> plugins -> available plugin tab -> select visual GC -> install. After finish install, close and reopen to see the new tab "Visual GC" should appear.
d. Go to the Monitor tab -> Heap Dump, will create Heap dump file for further analyzing.
Using the Memory Analyzer tool
a. Download MAT from eclipse.org/mat
b. Open the Heap dump file by MAT.
** Hit below error when start MAT on MAC:
See the log file
/Users/$USER/.eclipse/227783881_macosx_cocoa_x86_64/configuration/1554282657960.log
!ENTRY org.eclipse.osgi 4 0 2019-04-03 17:11:18.252
10 !MESSAGE Application error
11 !STACK 1
12 java.lang.IllegalStateException: The platform metadata area could not be written: /privat e/var/folders/cp/bnmgrhws25b_3272ly4zn0d00000gn/T/AppTranslocation/94A1F89C-EF58-45B4-A82 5-3D7DCEF5963D/d/mat.app/Contents/MacOS/workspace/.metadata. By default the platform wri tes its content
13 under the current working directory when the platform is launched. Use the -data parameter to
14 specify a different content area for the platform.
Solution:
$ vi mat.app/Contents/Eclipse/MemoryAnalyzer.ini
edit your MemoryAnalyzer.ini file, and add those lines (on two separate lines, and before the vm arguments), replacing the second with the path where you want the MemoryAnalyzer to write its content:
-data
<desired path of your working directory>
PermGen and the Metaspace
- In the Java 6, there is a further part of the Heap called PermGen. PermGen is nerver garbage-collected. There are two types of objects that go to PermGen, internized strings, and metadata for class.
- From Java 7, internized strings are no longer stored in PermGen, this change means that the internized strings are in the old part of the Heap, and can be garbage-collected.
- From Java 8, they removed the PermGen altogether, instead created something else called MetaSpace, which is where the class metadata are placed.
- The MetaSpace is a separate area of memory and it's not part of the Heap. Instead it's allocated out the host's native memory so the maximum available space for the MetaSpace is the total available system memory for the computer.
Tuning the Virtual Machine
Heap size
-Xmx, -Xms are non-standard argumants, so they are not guaranteed to be implemented on every runtime.
Please reference below page for the Java command line options.
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html
Garbage collection and generation sizes
-verbose:gc
-Xmn : set the size of the young generation
Choosing a garbage collector
- Serial : a single thread
- Parallel : multiple threads running.
- Mostly Concurrent: the stop-the-world part of the GC process is mimimized.
Java Memory Management的更多相关文章
- Java Memory Management(1)
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...
- 转)Understanding Java Memory Management
Understanding Java Memory Management - IBM Java Native Interface (JNI) Objects and Code Java Native ...
- Java Memory Management Skill List
Java内存管理小技巧: 尽量使用直接量 当需要使用字符串,还有Byte,Short,Integer,Long,Float,Double,Boolean,Character包装类的实例时,程序不应该采 ...
- Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...
- Understanding Java Memory Model-理解java内存模型(JVM)
from https://medium.com/platform-engineer/understanding-java-memory-model-1d0863f6d973 Understanding ...
- jmap命令(Java Memory Map)(转)
JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...
- Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1
from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...
- The Introduction of Java Memory Leaks
One of the most significant advantages of Java is its memory management. You simply create objects a ...
- Understanding Memory Management(2)
Understanding Memory Management Memory management is the process of allocating new objects and remov ...
随机推荐
- 看一眼就学会的 HTML 小游戏搭建!
本文作者:CODING 用户 - xfly 身边经常会有小伙伴问我有没有办法不买服务器也能上线自己的个人项目,比如不少同学都非常喜欢搭建一个属于自己的博客站点或者小游戏等. 目前相对比较简便的且不花自 ...
- Flutter 异常处理之图片篇
背景 说到异常处理,你可能直接会认为不就是 try-catch 的事情,至于写一篇文章单独来说明吗? 如果你是这么想的,那么本篇说不定会给你惊喜哦~ 而且本篇聚焦在图片的异常处理. 场景 学以致用,有 ...
- sql sever insert into混合嵌套插入
如果你想插入的字段取值方式不同,既有自己设定的值,又想插入某个表中的某个字段数据,下面就举例说明 insert into Meters(metertypeid, meternumber, consta ...
- 安装windows 10到固态硬盘实践记录
1.前提 由于之前一直用的机械硬盘,电脑用了几年是越来越慢,所以打算买个SSD,装个新系统,其他的机械硬盘都当从盘用 2.准备工作 SSD :256G 3星的 WIN10正版光盘一张 外置光驱一个 3 ...
- 网络流 P3358 最长k可重区间集问题
P3358 最长k可重区间集问题 题目描述 对于给定的开区间集合 I 和正整数 k,计算开区间集合 I 的最长 k可重区间集的长度. 输入输出格式 输入格式: 的第 1 行有 2 个正整数 n和 k, ...
- Project file is incomplete. Expected imports are missing 错误解决方案
当你打开一个.net core的项目,Visual Studio 可能无法打开,提示如下错误: D:\workshop\Github\Ocelot\src\Ocelot\Ocelot.csproj : ...
- 【STM32H7教程】第3章 STM32H7整体把控
完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第3章 STM32H7整体把控 初学STM32H7一 ...
- 为什么分库分表使用2的N次方 一个字节用两位16进制
你说说为神马表的总数.redis库的总数.HashMap的数量最好是2的N次方 数据在表库HashMap 落地时候都会跟总数取模,这个我们做个测试 假设数量是2的3次方就是8,即索引就是0-7 php ...
- Docker 查看镜像信息
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发个人网站: https://ww ...
- FragmentTabHostTopDemo【FragmentTabHost固定宽度且居中】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(居中且宽度非全屏)展现. 备注:该Demo主要是演示FragmentTabHost ...