1. 程式人生 > >Java 記憶體洩露 Memory Leak(Oracle官方文件)

Java 記憶體洩露 Memory Leak(Oracle官方文件)

The jmap -permgen command prints statistics for the objects in the permanent generation, including information about internalized String instances. See   2.7.4 Getting Information on the Permanent Generation.

3.1.3 Detail Message: Requested array size exceeds VM limit

The detail message Requested array size exceeds VM limit

 indicates that the application (or APIs used by that application) attempted toallocate an array that is larger than the heap size. For example, if an application attempts to allocate an array of 512MB but the maximum heap size is 256MB then OutOfMemoryError will be thrown with the reason Requested array size exceeds VM limit
. In most cases the problem is either a configuration issue (heap size too small), or a bug that results in an application attempting to create a huge array, for example, when the number of elements in the array are computed using an algorithm that computes an incorrect size.

3.1.4 Detail Message: request <size> bytes for <reason>. Out of swap space?

The detail message request <size> bytes for <reason>. Out of swap space? appears to be an OutOfMemoryError. However, the HotSpot VM code reports this apparent exception when an allocation from the native heap failed and the native heap might be close to exhaustion. The message indicates the size (in bytes) of the request that failed and the reason for the memory request. In most cases the <reason> part of the message is the name of a source module reporting the allocation failure, although in some cases it indicates a reason.

When this error message is thrown, the VM invokes the fatal error handling mechanism, that is, it generates a fatal error log file, which contains useful information about the thread, process, and system at the time of the crash. In the case of native heap exhaustion, the heap memory and memory map information in the log can be useful. See   Appendix C, Fatal Error Log for detailed information about this file.

If this type of OutOfMemoryError is thrown, you might need to use troubleshooting utilities on the operating system to diagnose the issue further. See   2.16 Operating-System-Specific Tools.

The problem might not be related to the application, for example:

The operating system is configured with insufficient swap space.
Another process on the system is consuming all memory resources.

If neither of the above issues is the cause, then it is possible that the application failed due to a native leak, for example, if application or library code is continuously allocating memory but is not releasing it to the operating system.

3.1.5 Detail Message: <reason> <stack trace> (Native method)

If the detail part of the error message is <reason> <stack trace> (Native method) and a stack trace is printed in which the top frame is a native method, then this is an indication that a native method has encountered an allocation failure. The difference between this and the previous message is that the allocation failure was detected in a JNI or native method rather than in Java VM code.

If this type of OutOfMemoryError is thrown, you might need to use utilities on the operating system to further diagnose the issue. See   2.16 Operating-System-Specific Tools.

3.2 Crash Instead of OutOfMemoryError

Sometimes an application crashes soon after an allocation from the native heap fails. This occurs with native code that does not check for errors returned by memory allocation functions.

For example, the malloc system call returns NULL if there is no memory available. If the return from malloc is not checked, then the application might crash when it attempts to access an invalid memory location. Depending on the circumstances, this type of issue can be difficult to locate.

However, in some cases the information from the fatal error log or the crash dump might be sufficient to diagnose this issue. The fatal error log is covered in detail in   Appendix C, Fatal Error Log. If the cause of a crash is determined to be the failure to check an allocation failure, then the reason for the allocation failure must be examined. As with any other native heap issue, the system might be configured with insufficient swap space, another process on the system might be consuming all memory resources, or there might be a leak in the application (or in the APIs that it calls) that causes the system to run out of memory.

3.3 Diagnosing Leaks in Java Language Code

Diagnosing leaks in Java language code can be a difficult task. In most cases it requires very detailed knowledge of the application. In addition the process is often iterative and lengthy. This section provides the following subsections:

 3.3.1 NetBeans Profiler
 3.3.2 Using the jhat Utility
 3.3.3 Creating a Heap Dump
 3.3.4 Obtaining a Heap Histogram on a Running Process
 3.3.5 Obtaining a Heap Histogram at OutOfMemoryError
 3.3.6 Monitoring the Number of Objects Pending Finalization
 3.3.7 Third Party Memory Debuggers

3.3.1 NetBeans Profiler

The NetBeans Profiler (previously known as JFluid) is an excellent profiler, which can locate memory leaks very quickly. Most commercial memory leak debugging tools can often take a long time to locate a leak in a large application.The NetBeans Profiler, however, uses the pattern of memory allocations and reclamations that such objects typically demonstrate. This process includes also the lack of memory reclamations. The profiler can check where these objects were allocated, which in many cases is sufficient to identify the root cause of the leak.

3.3.2 Using the jhat Utility

The jhat utility (see   2.5 jhat Utility ) is useful when debugging unintentional object retention (or memory leaks). It provides a way to browse an object dump(垃圾場), view all reachable objects in the heap, and understand which references are keeping an object alive.

To use jhat you must obtain one or more heap dumps of the running application, and the dumps must be in binary format. Once the dump file is created, it can be used as input to jhat, as described in   2.5 jhat Utility .

3.3.3 Creating a Heap Dump

A heap dump provides detailed information on the allocation of heap memory. The following sections describe several ways to produce a heap dump:

 3.3.3.1 HPROF Profiler
 3.3.3.2 jmap Utility
 3.3.3.3 JConsole Utility
 3.3.3.4 -XX:+HeapDumpOnOutOfMemoryError Command-line Option
3.3.3.1 HPROF Profiler

The HPROF profiler agent can create a heap dump while the application is executing. The following is an example of the command line:

$  
         java -agentlib:hprof=file=snapshot.hprof,format=b  
         application
      

If the VM is embedded or is not started using a command line launcher that allows additional options to be provided, then it might be possible to use the JAVA_TOOLS_OPTIONS environment variable so that the -agentlib option is automatically added to the command line. See   A.2JAVA_TOOL_OPTIONS Environment Variable for further information on this environment variable.

Once the application is running with HPROF, a heap dump is created by pressing Ctrl-\ or Ctrl-Break (depending on the platform) on the application console. An alternative approach on Solaris OS and Linux is to send a QUIT signal with the kill -QUIT pid command. When the signal is received, a heap dump is created; in the above example the file snapshot.hprof is created.

The heap dump file contains all the primitive data and stack traces.

A dump file can contain multiple heap dumps. If Ctrl-\ or Ctrl-Break is pressed a number of times then the subsequent dumps are appended to the file. The jhat utility uses the # n syntax to distinguish the dumps, where n is the dump number.

3.3.3.2 jmap Utility

A heap dump can also be obtained using the jmap utility (see   2.7 jmap Utility ). The following is an example of the command line:

$  
         jmap -dump:format=b,file=snapshot.jmap  
         process-pid
      

Regardless of how the Java VM was started, the jmap tool will produce a head dump snapshot, in the above example in a file called snapshot.jmap. The jmap output files should contain all the primitive data, but will not include any stack traces showing where the objects have been created.

3.3.3.3 JConsole Utility

Another way to obtain a heap dump is with the JConsole utility. In the MBeans tab, select theHotSpotDiagnostic MBean, then the Operations display, and choose the dumpHeap operation.

3.3.3.4 -XX:+HeapDumpOnOutOfMemoryError Command-line Option

If you specify the -XX:+HeapDumpOnOutOfMemoryError command-line option, and if anOutOfMemoryError is thrown, the VM generates a heap dump.

3.3.4 Obtaining a Heap Histogram on a Running Process

You can try to quickly narrow down a memory leak by examining a heap histogram. This information can be obtained in several ways:

A heap histogram can be obtained from a running process using the command jmap -histopid. The output shows the total size and instance count for each class type in the heap. If a sequence of histograms is obtained (for example, every 2 minutes), then you might be able to observe a trend that can lead to further analysis.
On Solaris OS and Linux, the jmap utility can also provide a histogram from a core file.
If the Java process is started with the -XX:+PrintClassHistogram command-line option, then the Ctrl-Break handler will produce a heap histogram.

3.3.5 Obtaining a Heap Histogram at OutOfMemoryError

If you specify the -XX:+HeapDumpOnOutOfMemoryError command-line option, and if anOutOfMemoryError is thrown, the VM generates a heap dump. You can then use the jmap utility to obtain a histogram from the heap dump.

If a core file is produced when the OutOfMemoryError is thrown, you can execute jmap on the core file to get a histogram, as in the following example.

$  
         jmap -histo \ /java/re/javase/6/latest/binaries/solaris-sparc/bin/java core.27421

Attaching to core core.27421 from executable 
/java/re/javase/6/latest/binaries/solaris-sparc/bin/java, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 1.6.0-beta-b63
Iterating over heap. This may take a while...
Heap traversal took 8.902 seconds.

Object Histogram:
 
Size      Count   Class description
-------------------------------------------------------
86683872  3611828 java.lang.String
20979136  204     java.lang.Object[]
403728    4225    * ConstMethodKlass
306608    4225    * MethodKlass
220032    6094    * SymbolKlass
152960    294     * ConstantPoolKlass
108512    277     * ConstantPoolCacheKlass
104928    294     * InstanceKlassKlass
68024     362     byte[]
65600     559     char[]
31592     359     java.lang.Class
27176     462     java.lang.Object[]
25384     423     short[]
17192     307     int[]
:
      

The example shows that the OutOfMemoryError is caused by the number of java.lang.Stringobjects (3611828 instances in the heap). Without further analysis it is not clear where the strings are allocated. However, the information is still useful and the investigation can continue with tools such as HPROF or jhat to find out where the strings are allocated, as well as what references are keeping them alive and preventing them from being garbage collected.

3.3.6 Monitoring the Number of Objects Pending Finalization

As noted in   3.1.1 Detail Message: Java heap space , excessive use of finalizers can be the cause of OutOfMemoryError. You have several options for monitoring the number of objects that are pending finalization.

The JConsole management tool (see  2.3 JConsole Utility) can be used to monitor the number of objects that are pending finalization. This tool reports the pending finalization count in the memory statistics on the “Summary” tab pane. The count is approximate but it can be used to characterize an application and understand if it relies a lot on finalization.
On Solaris OS and Linux, the jmap -finalizerinfo option prints information on objects awaiting finalization.
An application can report the approximate number of objects pending finalization using thegetObjectPendingFinalizationCount method in thejava.lang.management.MemoryMXBean class. Links to the API documentation and example code can be found in  2.17 Developing Diagnostic Tools. The example code can easily be extended to include the reporting of the pending finalization count.

3.3.7 Third Party Memory Debuggers

In addition to the tools mentioned in the previous chapters, there are a large number of third-party memory debuggers available. JProbe from Quest Software, and OptimizeIt from Borland are two examples of commercial tools with memory debugging capability. There are many others and no specific product is recommended.

3.4 Diagnosing Leaks in Native Code

Several techniques can be used to find and isolate native code memory leaks. In general there is no single ideal solution for all platforms.

3.4.1 Tracking All Memory Allocation and Free Calls

A very common practice is to track all allocation and free calls of the native allocations. This can be a fairly simple process or a very sophisticated one. Many products over the years have been built up around the tracking of native heap allocations and the use of that memory.

Tools like Purify and Sun's 

相關推薦

Java 記憶體洩露 Memory LeakOracle官方

The jmap -permgen command prints statistics for the objects in the permanent generation, including information about internalized String instances. S

Solr Indexingsolr 官方

solr的索引可以接受不同的資料來源,包括:xml,csv檔案,資料庫表和常見的檔案格式的檔案(word,PDF) 有三種常用的方式來載入資料到solr的索引中: 1、Solr Cell 框架提取二進位制檔案或結構化檔案,如office word,PDF等 2、向solr伺

Mysql優化出自官方 - 第三篇

目錄 Mysql優化(出自官方文件) - 第三篇 1 Multi-Range Read Optimization(MRR) 2 Block Nested-Loop(BNL) and Batched Key Access Jo

Mysql優化出自官方 - 第八篇索引優化系列

目錄 Mysql優化(出自官方文件) - 第八篇(索引優化系列) Optimization and Indexes 1 Foreign Key Optimization 2 Column Indexes

Mysql優化出自官方 - 第九篇優化資料庫結構篇

目錄 Mysql優化(出自官方文件) - 第九篇(優化資料庫結構篇) 1 Optimizing Data Size 2 Optimizing MySQL Data Types 3 Optimizing for

利用linux的mtrace命令定位記憶體洩露(Memory Leak)

1、安裝mtrace工具 centos : sudo yum install glibc-utils 2、mtrace工具使用 /************************************************************************* >

效能優化之記憶體洩露(Memory Leak)解決

1 分析記憶體洩漏遇到的問題 (1)把兩個dump檔案對比,找出GC root樹,發現MainActivity例項被CommonUtil引用,說懷疑此處可能有洩露。但實際開發的時候,很多這種情況,莫非都要懷疑一遍?我們必然知道mat只是個工具,提供洩露的建議,

Java_記憶體溢位(Memory Overflow)和記憶體洩露(Memory Leak)的區別

via: http://www.cnblogs.com/fryy/archive/2013/11/27/3445281.html 記憶體洩漏指你用malloc或new申請了一塊記憶體,但是沒有通過free或delete將記憶體釋放,導致這塊記憶體一直處於佔用狀態 記憶

JAVA 記憶體洩露詳解原因、例子及解決

  Java的一個重要特性就是通過垃圾收集器(GC)自動管理記憶體的回收,而不需要程式設計師自己來釋放記憶體。理論上Java中所有不會再被利用的物件所佔用的記憶體,都可以被GC回收,但是Java也存在記憶體洩露,但它的表現與C++不同。 JAVA 中的記憶體管理

Oracle 10g DataGuard 監視主資料庫和備用資料庫官方

-- 監視主資料庫和備用資料庫 --- -- 動態效能檢視(固定檢視)--- -- 監控流程活動 SELECT PROCESS, CLIENT_PROCESS, SEQUENCE#, STATUS FROM V$MANAGED_STANDBY; -- 確定重做申請的進度

java中的反射 1—— 簡介@譯自Oracle官方

反射API的使用 反射通常被用在需要檢查或修改其他執行在JVM中的程式的執行時行為的程式中。這是一種高階特性所以只建議對Java語言基礎有深刻理解的開發人員使用。請牢記這個忠告,反射是一種強大的技術,它使得程式可以完成用其他手段不可能完成的任務。 可擴充套件性程式可以通過使用類的完全限定名稱(fully-

Java I/O---RandomAccessFile類隨機訪問的讀取和寫入

通過 bubuko system 姓名 println and row param 指向 1.JDK API中RandomAccessFile類的描述 此類的實例支持對隨機訪問文件的讀取和寫入。隨機訪問文件的行為類似存儲在文件系統中的一個大

vue基礎學習官方

生命周期 阻止 code 動態 file display 按鍵 返回 版本 基礎 介紹 是什麽 是一套用於構建用戶界面的漸進式框架 聲明式渲染 <div id="app">{{ message }}</div> var app =

Java學習不走彎路教程3.從內容查詢開始

輔助 pass 多說 font sys sta case index exe 一. 前言在前兩章教程中,分別介紹了DOS環境搭建和Eclipse環境搭建。本章將帶大家實現用簡單SQL語句查詢文件。註:1.本文針對初學Java的同學訓練學習思路,請不要太糾結於細節問題。2.本

spark 調優官方

1.序列化 物件在進行網路傳輸或進行持久化時需要進行序列化,如果採用序列化慢或者消耗大量位元組的序列化格式,則會拖慢計算。 spark 提供了兩種序列化類庫 1). Java serialization 靈活,但是很慢 2) Kryo serializati

HDFS架構指南Hadoop官方翻譯

HDFS架構指南 本文翻譯自《HDFS Architecture Guide》 來源於Apache開源社群的Hadoop Apache Project 文獻引用為: Borthakur D. HDFS architecture guide[J]. Hadoop

oracle官方-temporary table

Temporary Tables In addition to permanent tables, Oracle Database can create temporary tables to hold session-private data that exists onl

oracle官方-create profile

CREATE PROFILE Note: Oracle recommends that you use the Database Resource Manager rather than this SQL statement to establish resource limits. The

oracle官方-index

Overview of Indexes Indexes are optional structures associated with tables and clusters. You can create indexes on one or more columns of a table to

oracle官方-手工建庫詳解

Creating a Database with the CREATE DATABASE Statement Using the CREATE DATBASE SQL statement is a more manual approach to creating a