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 ...
随机推荐
- css常见的各种布局下----三列布局
css 三列布局,左右固定宽度右边自适应 1不使用定位,只使用浮动可以实现左右固定,中间宽度自适应布局 1.1.1 自适应部分一定要放第一个位子,使用浮动,并且设置宽度为100%,不设置浮动元素内容不 ...
- NFS挂载异常 mount.nfs: Input/output error
[root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...
- Variable SQLLOGDIR not found
昨天在一数据库(SQL Server 2008 R2 SP3)上部署了一个作业,今天早上收到告警邮件,作业执行报错"Unable to start execution of step 1 ( ...
- 为什么在STM32F429工程配置中需要预先定义USE_STDPERIPH_DRIVER和STM32F429_439xx?
如图: 1.查找USE_STDPERIPH_DRIVER,发现这个宏出现在stm32f4xx.h头文件中,并且有如下代码: 也就是说,通过已经定义了USE_STDPERIPH_DRIVER宏加载stm ...
- 如何修复使用WSUS进行升级Win 10 更新1809的报错(0x8024200)
备注:该文档有另一个姊妹篇,介绍如何修复下游服务器一直有处于需要文件下载的状况. /* Style Definitions */ table.MsoNormalTable {mso-style-nam ...
- C#异步编程----Thread
一.问题由来 多线程能实现的基础: 1.CPU运行速度太快,硬件处理速度跟不上,所以操作系统进行分时间片管理.这样,宏观角度来说是多线程并发 ,看起来是同一时刻执行了不同的操作.但是从微观角度来讲,同 ...
- Scala设计模式
尽管Scala还有一些基于语言特性的设计模式,单本文还是着重于介绍大家所周知的经典设计模式,因为这些设计模式被认为是开发者之间交流的工具. 创建型设计模式 1.工厂方法模式 2.延迟加载模式 3.单例 ...
- Python开发:Python2和Python3的共存和切换使用
从python2到python3,这两个版本可以说是从语法.编码等多个方面上都有很大的差别.为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下相容,也就是说许多针对早期Python2版 ...
- k.tt 研究下生成的逻辑代码:从壹开始前后端分离 [.netCore 填坑 ] 三十二║ 四种方法快速实现项目的半自动化搭建
更新 1.更新小伙伴 @大龄Giser 提出好点子:试试VS的插件扩展:VSIX.ItemProject等,将T4模板给制作插件,这里先记下,有懂的小伙伴可以自己先试试,我会在以后更新. 2.感谢小伙 ...
- .NET Core TDD 前传: 编写易于测试的代码 -- 构建对象
该系列第1篇: 讲述了如何创造"缝". "缝"(seam)是需要知道的概念. 本文是第2篇, 介绍的是如何避免在构建对象时写出不易测试的代码. 本文的概念性内 ...