• 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的更多相关文章

  1. Java Memory Management(1)

    Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...

  2. 转)Understanding Java Memory Management

    Understanding Java Memory Management - IBM Java Native Interface (JNI) Objects and Code Java Native ...

  3. Java Memory Management Skill List

    Java内存管理小技巧: 尽量使用直接量 当需要使用字符串,还有Byte,Short,Integer,Long,Float,Double,Boolean,Character包装类的实例时,程序不应该采 ...

  4. Java (JVM) Memory Model – Memory Management in Java

    原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...

  5. Understanding Java Memory Model-理解java内存模型(JVM)

    from https://medium.com/platform-engineer/understanding-java-memory-model-1d0863f6d973 Understanding ...

  6. jmap命令(Java Memory Map)(转)

    JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...

  7. 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 ...

  8. The Introduction of Java Memory Leaks

    One of the most significant advantages of Java is its memory management. You simply create objects a ...

  9. Understanding Memory Management(2)

    Understanding Memory Management Memory management is the process of allocating new objects and remov ...

随机推荐

  1. 19,CSS 滤镜

    1.Filter 属性介绍 2.Alpha 滤镜的使用 3.Blur 滤镜的使用 4.Filph.Filpv 滤镜 5.DropShadow 滤镜 6.Glow 滤镜 7.Gray ,Invert,X ...

  2. ArcGIS API for JavaScript 4.9 & 3.26 发布与新特性

    应该是中文首发?我只想说:更新太TMD快了 QAQ Part I -- JsAPI 4.9 主题1:在2D中默认启用WebGL渲染 在要素图层.CSV图层.流图层中是使用WebGL渲染的,这个任务由M ...

  3. ArcGIS消除图斑重叠错误

    在生产中,经常会遇见有图斑重叠这种拓扑错误的矢量,大部分情况下,需要人工比对影像处理.但是如果只需要用到这些矢量的形状.面积,可以在ArcMap中用以下方法,快速消除图斑重叠错误,不必手工处理. 如下 ...

  4. Android自动解析html带图片,实现图文混排

    在android中,如何将html代码转换为text,然后显示在textview中呢,有一个简单直接的方法: textView.setText(Html.fromHtml(content)); 然而用 ...

  5. 记录一些flutter学习网址

    字体图标生成 http://fluttericon.com/Flutter中文网 https://flutterchina.club Flutter官网 https://flutter.ioFlutt ...

  6. windows 结束端口占用

    1. cmd 调出命令行窗口 2. netstat -ano  查看端口被占用情况 3.命令:tasklist | findstr "9480"  发现被httpd.exe 占用 ...

  7. 配置IIS网站,我遇到的那些坑~

    配置错误 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 over ...

  8. 使用kubeadm平滑地升级kubenetes集群(v1.10.2到v1.10.3)

    写在前面 kubernetes是目前最炙手火热的容器管理.调度.编排平台,不仅拥有全面的生态系统,而且还开源.开放自治,谁贡献的多,谁的话语权就大,云提供商都有专门的工程师来从事kubernetes开 ...

  9. Amqp整合com.rabbitmq.client.ShutdownSignalException: channel error; protocol method异常处理

    java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:126) at com.rabbitmq ...

  10. MySQL 中 update 修改数据与原数据相同会再次执行吗?

    阅读本文大概需要 2.8 分钟. 作者:powdba 来源:阿里云栖社区 一.背景 本文主要测试MySQL执行update语句时,针对与原数据(即未修改)相同的update语句会在MySQL内部重新执 ...