分类目录归档:Java

Inside JVM–GC(Garbage collection)

一、Common Problems in Java SE Application

This message is based on the following materials.
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/#Insufficient_Memory
http://java.sun.com/developer/technicalArticles/javase/troubleshoot//

1. Java Memory Basic

1.1 Heap, Non-Heap, and Native

Heap memory is the runtime data area from which memory for all class instances and arrays is allocated.
Non-heap memory includes the method area and memory required for the internal processing or optimization of the JVM.
It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors.
Native memory is the virtual memory managed by the operating system. When the memory is insufficient for an application to allocate, a java.lang.OutOfMemoryError will be thrown. 继续阅读

Java HotSpot VM Options

http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp

This document provides information on typical command-line options and environment variables that can affect the performance characteristics of the Java HotSpot Virtual Machine. Unless otherwise noted, all information in this document pertains to both the Java HotSpot Client VM and the Java HotSpot Server VM.

Users of JDKs older than 1.3.0 who wish to port to a Java HotSpot VM, should see Java HotSpot Equivalents of Exact VM flags. 继续阅读

JVM Options For GC

JVM Options For GC
1: heap size
a: -Xmx
指定jvm的最大heap大小,如:-Xmx2g

b: -Xms
指定jvm的最小heap大小,如:-Xms1g

c: -Xmn
指定jvm中New Generation的大小,如:-Xmn256m

d: -XX:PermSize
指定jvm中Perm Generation的最小值,如:-XX:PermSize=32m

e: -XX:MaxPermSize
指定Perm Generation的最大值,如:-XX:MaxPermSize=64m

f: -Xss
指定线程桟大小,如:-Xss128k

g: -XX:NewRatio
指定jvm中Old Generation heap size与New Generation的比例,在使用CMS GC的情况下此参数失效, 如:-XX:NewRatio=2

h: -XX:SurvivorRatio
指定New Generation中Eden Space与一个Survivor Space的heap size比例,-XX:SurvivorRatio=8,那么在总共New Generation为10m的情况下,Eden Space为8m

i: -XX:MinHeapFreeRatio
指定jvm heap在使用率小于n的情况下,heap进行收缩,Xmx==Xms的情况下无效,如:-XX:MinHeapFreeRatio=30

j: -XX:MaxHeapFreeRatio
指定jvm heap在使用率大于n的情况下,heap进行扩张,Xmx==Xms的情况下无效,如:-XX:MaxHeapFreeRatio=70

k: -XX:LargePageSizeInBytes
指定Java heap的分页页面大小,如:-XX:LargePageSizeInBytes=128m 继续阅读

Garbage Collection实践总结

Available Collectors

The discussion to this point has been about the serial collector. The Java HotSpot VM includes three different collectors, each with different performance characteristics.

  1. The serial collector uses a single thread to perform all garbage collection work, which makes it relatively efficient since there is no communication overhead between threads. It is best-suited to single processor machines, since it cannot take advantage of multiprocessor hardware, although it can be useful on multiprocessors for applications with small data sets (up to approximately 100MB). The serial collector is selected by default on certain hardware and operating system configurations, or can be explicitly enabled with the option -XX:+UseSerialGC.
  2. The parallel collector (also known as the throughput collector) performs minor collections in parallel, which can significantly reduce garbage collection overhead. It is intended for applications with medium- to large-sized data sets that are run on multiprocessor or multi-threaded hardware. The parallel collector is selected by default on certain hardware and operating system configurations, or can be explicitly enabled with the option -XX:+UseParallelGC.
    • New: parallel compaction is a feature introduced in J2SE 5.0 update 6 and enhanced in Java SE 6 that allows the parallel collector to perform major collections in parallel. Without parallel compaction, major collections are performed using a single thread, which can significantly limit scalability. Parallel compaction is enabled by adding the option -XX:+UseParallelOldGC to the command line.
  3. The concurrent collector performs most of its work concurrently (i.e., while the application is still running) to keep garbage collection pauses short. It is designed for applications with medium- to large-sized data sets for which response time is more important than overall throughput, since the techniques used to minimize pauses can reduce application performance. The concurrent collector is enabled with the option -XX:+UseConcMarkSweepGC.

继续阅读

JVM Garbage Collection

JVM的自动垃圾收集(Garbage Collection)使得开发人员无需关注垃圾收集的细节,不过,当内存问题成为系统瓶颈的时候,我们就需要了解一下JVM的垃圾收集机制了。

应用程序中生成的对象绝大部分都是临时对象,属于那种生的快死的快的,来也匆匆,去也匆匆,当然也有伴随应用程序的生命周期而存在的对象,鉴于对象的生命周期的不同,JVM的内存是分代(Generation)管理的。如果把JVM看作一个战场,把各个对象看作士兵,那么大部分的士兵刚投入战场不久就英勇就义,只有少部分的士兵能够在战场中摸爬滚打能够坚持一段时间。(史上最残忍的战场,阿弥陀佛,善哉!善哉!)。分代情况如下图所示:

default arrangement of generations

其中,年轻代(Young Generation)包含伊甸园(Eden)和2段幸存者空间(Survivor Spaces),伊甸园当然是新生对象的乐园(只在乎曾经拥有,不在乎天长地久。也许看作前线战场更为合适:)(也有部分大对象直接分配在年老代)。而幸存者空间是用来保存上次GC之后依然存活的对象,经过战事的洗礼,能够存活下来实属不易,说明你是一个优秀的士兵,特此晋升一下,并且分配给你一间前线作战指挥所。2段空间保持其中一个为空,后面再详细说明缘由和作用。 继续阅读

jconsole+tomcat配置说明

最近需要参与一些java程序debug和性能调整方面的工作,jconsole是jdk自带的工具,比较好用,以下文章前面大部分翻译自:http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html ,后面关于用户名/密码和使用ssl加密连接的部分是从其他地方搜集的资料并且进行了验证。

Jconsole简介:

Jconsole是一个JMX兼容的监视工具。它使用Java虚拟机的JMX机制来提供运行在Java平台的应用程序的性能与资源耗费信息。 继续阅读

jvmstat 介绍

Solaris and Linux Installation Instructions

J2SE Installation

If you have not already downloaded and installed the J2SE 5.0 JDK, please do so now. Follow the J2SE 5.0 JDK download and installation instructions. Once you have a working J2SE 5.0 JDK installation, continue with jvmstat Tools Installation. Note that the J2SE 5.0 JRE does not contain the class files need to run the jvmstat 3.0 tools. You must download the J2SE 5.0 JDK to run the jvmstat 3.0 tools. 继续阅读

JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jsta

JVM监控工具介绍

jstack — 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。另外,jstack工具还可以附属到正在运行的java程序中,看到当时运行的java程序的java stack和native stack的信息, 如果现在运行的java程序呈现hung的状态,jstack是非常有用的。目前只有在Solaris和Linux的JDK版本里面才有。 继续阅读

JProfiler 5.1.4的使用方法

1. JProfiler运行环境配置

安装目录结构如下,子目录中显示了支持的操作系统:

在服务器和客户端都要安装JProfiler,并且要安装License,在分析工具客户端中进行安装。Windows环境中运行客户端程序:

    jprofiler5.1.4\bin\jprofiler.exe

UNIX系统中运行客户端程序,执行shell脚本:

    jprofiler5.1.4\bin\jprofiler

需要安装License之后才能使用,请使用合法License

继续阅读