• 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. 查看多核CPU各核的状态

    1 top  命令,然后按数字“1” 2  命令:mpstat -P ALL  3 命令:sar -P ALL 输出较多,可grep或者重定向至文件查看 个人推荐使用第二种方式,操作方便且输出较少,看 ...

  2. 在地图中调用显示FeatureLayer并进行render、popupTemplate、添加图例等相关内容的设置

    ArcGIS Server发布完FeatureLayer后,就可以在自己的代码中调用并在地图上显示出来了. 一.代码框架 调用FeatureLayer,要在require开头引入"esri/ ...

  3. python3 进一步了解装饰器 NLP第四条

    还是先来抄一段NLP第四条: 四,只有感官经验塑造出来的世界,没有绝对的真实世界   每个人运用自己的感觉器官把资料摄入(摄入过程),由于感官运用是主观地有选择性的,因此不能,亦不需要把所有资料捕获. ...

  4. kubernetes 客户端KubeClient使用及常用api

    KubeClient是kubernetes 的C#语言客户端简单易用,KubeClient是.NET Core(目标netstandard1.4)的可扩展Kubernetes API客户端, gith ...

  5. ReactNative之结合具体示例来看RN中的的Timing动画

    今天继续更新RN相关的博客.上篇博客详细的聊了RN中关于Flex布局的相关东西,具体请参见<ReactNative之参照具体示例来看RN中的FlexBox布局>.本篇博客继续更新RN的动画 ...

  6. MySQL学习(三)主备分库分表和恢复数据 --- 2019年2月

    1.MySQL主备切换 readonly 设置对超级(super)权限是无效的,而用于同步更新的线程,就拥有超级权限. 建议在做主备数据库的时候,将备用数据库设置为只读.(反向用readonly来判断 ...

  7. 关于用户与服务端密码的校验问题 !mysql php

    问题:如何拿到服务端的数据与客户端的数据进行对比! 判断是否一致: 问题解决步骤: 建立数据库连接: $conn = mysqli_connect(主机地址,用户名,用户密码,数据库名字): 查询数据 ...

  8. .NET Core微服务之基于Consul实现服务治理

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Consul基础介绍 Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发 ...

  9. BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...

  10. Redis五大数据类型的常用操作

    在上一篇博文<centos安装redis>中,已经详细介绍了如何在centos上安装redis,今天主要介绍下Redis五大数据类型及其五大数据类型的相关操作. Redis支持五种数据类型 ...