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 ...
随机推荐
- 记一次mongodb聚合查询
先说一下场景,产品中用到了简单的表单构造器,开始提供了一系列的控件,例如单行文本框.多行文本框.单选.复选.时间等,之后你可以拖拽控件自己组装你想要的表单……网上有很多的表单构造器,这里就不细说了,可 ...
- python 线程(一)理论部分
Python线程 进程有很多优点,它提供了多道编程,可以提高计算机CPU的利用率.既然进程这么优秀,为什么还要线程呢?其实,仔细观察就会发现进程还是有很多缺陷的. 主要体现在一下几个方面: 进程只能在 ...
- windows根据端口号杀进程
有时候eclipse会被卡死, 结束进程后重新启动项目会出现端口已经被占用 这时候需要杀掉进程 1, cmd打开dos窗口 2, netstat -ano | findstr "端口号&qu ...
- Python爬虫 【requests】request for humans
安装 pip install requests 源码 git clone git://github.com/kennethreitz/requests.git 导入 import requests 发 ...
- odoo权限机制
转两篇关于权限的2篇文章,加深这方面的认识.注:后面附有原作者地址,希望不构成侵权. 第一篇:http://www.cnblogs.com/dancesir/p/6994030.html Odoo的权 ...
- 【RL-TCPnet网络教程】第39章 RL-TCPnet之TFTP服务器
第39章 RL-TCPnet之TFTP服务器 本章节为大家讲解RL-TCPnet的TFTP服务器应用,学习本章节前,务必要优先学习第38章的TFTP基础知识.有了这些基础知识之后,再搞本章节会 ...
- 技能提升丨Seacms 8.7版本SQL注入分析
有些小伙伴刚刚接触SQL编程,对SQL注入表示不太了解.其实在Web攻防中,SQL注入就是一个技能繁杂项,为了帮助大家能更好的理解和掌握,今天小编将要跟大家分享一下关于Seacms 8.7版本SQL注 ...
- 那些优秀的.NET开发者----汪宇杰:从重视细节,到成就技术专家
初识汪宇杰 在长沙.NET技术社区筹建过程中,溪源有幸认识来自上海的MVP汪宇杰Edi Wang.在中国众多的微软MVP中,Edi Wang作为一名九零后,也是一位年轻而充满才气的开发者,或许他或许外 ...
- PMBook - 6.项目进度管理
6.3 排列活动顺序 6.3.1 排列活动顺序:输入 6.3.1.1 项目管理计划 6.3.1.2 项目文件 6.3.1.3 事业环境因素 6.3.1.4 组织过程资产 6.3.2 排列活动顺序: ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十五 ║Vue基础:JS面向对象&字面量& this字
缘起 书接上文<从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十四 ║ VUE 计划书 & 我的前后端开发简史>,昨天咱们说到了以我的经历说明的web开发经历的 ...