Diagnosing and Resolving Error ORA-04031 on the Shared Pool or Other Memory Pools (文档 ID 146599.1)

In this Document

 Purpose
 Troubleshooting Steps
 1. Instance parameters related with the Shared Pool
 2. Diagnosing error ORA-04031
 3. Resolving error ORA-04031
 4. ORA-04031 error and Large Pool
 5. ORA-04031 and SHARED POOL FLUSHING
 6. Advanced analysis to ORA-04031 error
 Community Discussion
 References

APPLIES TO:

Oracle Database - Enterprise Edition - Version 8.1.7.4 and later
Information in this document applies to any platform.
***Checked for relevance on 24-Feb-2010***

PURPOSE

The purpose of this document is to provide an easy to use, step by step guide to resolving ORA-04031 errors.

In case of conflicting information between note and video presentation, information contained in the note takes precedence over information that may be recorded or shown on video. 

 Video - Diagnosing and Resolving 4031 errors (10:00) 

Note:
If you would like to explore this topic further, please join the Community discussion ' Diagnosing and Resolving ORA-4031 errors' where you can ask questions, get help from others, and share your experiences with this specific article.

TROUBLESHOOTING STEPS

When any attempt to allocate a large piece of contiguous memory in the shared pool fails Oracle first flushes all objects that are not currently in use from the pool and the resulting free memory chunks are merged. If there is still not a single chunk large enough to satisfy the request the ORA-04031 error is returned. NOTE: These errors can occur on an ASM instance as well. The default SHARED_POOL_SIZE should be sufficient in most environments, but can be increased if you are experiencing ORA-04031 errors.

The message that you will get when this error appears is the following:

04031, 00000, "unable to allocate %s bytes of shared memory (\"%s\",\"%s\",\"%s\",\"%s\")"

// *Cause: More shared memory is needed than was allocated in the shared
// pool.
// *Action: If the shared pool is out of memory, either use the
// dbms_shared_pool package to pin large packages,
// reduce your use of shared memory, or increase the amount of
// available shared memory by increasing the value of the
// INIT.ORA parameters "shared_pool_reserved_size" and
// "shared_pool_size".
// If the large pool is out of memory, increase the INIT.ORA
// parameter "large_pool_size".


1. Instance parameters related with the Shared Pool

Before continuing, understanding the following instance parameters will be essential:

  • SHARED_POOL_SIZE - This parameter specifies the size of the shared pool in bytes and can accept a numerical values or a number followed by the suffix "K" or "M" where "K" means "multiply by 1000" and "M" means "multiply by 1000000"
  • SHARED_POOL_RESERVED_SIZE - It specifies the shared pool space which is reserved for large contiguous requests for shared pool memory. This parameter along with the SHARED_POOL_RESERVED_MIN_ALLOC parameter, can be used to avoid the occurrence of this error from situations where shared pool fragmentation forces Oracle to search for and free 
    chunks of unused pool to satisfy the current request.

    Ideally, this parameter should be large enough to satisfy any request scanning for memory on the reserved list without flushing objects from the shared pool. Since the operating system memory may constraint the size of the shared pool, in general, you should set this parameter to 10% of the SHARED_POOL_SIZE parameter.
  • SHARED_POOL_RESERVED_MIN_ALLOC - The value of this parameter controls allocation of reserved memory. Memory allocation larger than this value can allocate space from the reserved list if a chunk of memory of sufficient size is not found on the shared pool free lists. The default value is adequate for most systems. If you increase the value, then the Oracle server will allow fewer allocations from the reserved list and will request more memory from the shared pool list. This parameter is hidden in Oracle 8i and higer, but it can be found by executing the following SQL statement:

    SELECT nam.ksppinm NAME,
           val.ksppstvl VALUE 
    FROM x$ksppi nam,
         x$ksppsv val
    WHERE nam.indx = val.indx
    AND nam.ksppinm LIKE '%shared%'
    ORDER BY 1;

    Note: This parameter was obsoleted with 8i. The parameter can still be modified via the underscore parameter _SHARED_POOL_RESERVED_MIN_ALLOC.
10g Note: In Oracle 10g a new feature called "Automatic Shared Memory Management" allows the DBA to reserve a pool of shared memory that is used to allocate the shared pool, the buffer cache, the java pool and the large pool. 

In general, when the database needs to allocate a large object into the shared pool and cannot find contiguous space available, it will automatically increase the shared pool size using free space from other SGA structure. 

Since the space allocation is automatically managed by Oracle, the probability of getting ora-4031 errors may be greatly reduced. Automatic Shared Memory Management is enabled when the parameter SGA_TARGET is greater than zero and the current setting can be obtained quering the V$SGA_DYNAMIC_COMPONENTS and V$SGA_RESIZE_OPS views. 

It is not possible restrict the size of auto-tuned components in the SGA if using SGA_TARGET. If you want to exercise more control on shared pool size, please do not use SGA_TARGET parameter. Using explicit settings for the auto-tuned components is recommended as minimum sizes to keep auto-tuning from being too aggressive moving memory in the SGA. 

Please refer to the 10g Administration Manual for further reference.

 

11g Note: In Oracle 11g a new feature called "Automatic Memory Mmanagement" allows the DBA to configure the database to manage shared memory areas as well as PGA memory. 

In general, when the database needs to allocate a large object into the shared pool and cannot find contiguous space available, it will automatically increase the shared pool size using free space from other SGA structure and/or PGA. During times of heavy workload, PGA may automatically increase using free memory from the SGA structures. 

Since the space allocation is automatically managed by Oracle, the probability of getting ora-4031 errors may be greatly reduced. Automatic Memory Management is enabled when the parameter MEMORY_TARGET is greater than zero and the current setting can be obtained quering the V$MEMORY_DYNAMIC_COMPONENTS and V$MEMORY_RESIZE_OPS views. 

It is not possible restrict the size of auto-tuned components in the SGA if using MEMORY_TARGET. If you want to exercise more control on the components in the SGA or PGA do not use MEMORY_TARGET or SGA_TARGET. SGA_TARGET and explicit settings for the auto-tuned components are recommended as minimum sizes to keep auto-tuning from being too aggressive moving memory for the database workload. 

Please refer to the 11g Administration Manual for further reference.

2. Diagnosing error ORA-04031

Note: most common ORA-4031 occurrences are related to the SHARED POOL SIZE, therefore the diagnostic steps provided in this article will mostly address issues related to the shared pool. For other areas like large_pool or java_pool where the memory allocation algorith is simpler, normally the error is caused by an undersized structure.

ORA-04031 error can be due to either an inadequeate sizing of the SHARED POOL size or due to heavy fragmentation leading the database to not finding large enough chuncks of memory. NOTE: There have been many reports where the default size for shared_pool_size on an ASM instance is too small. If you experience ORA-04031 error on your ASM instance, increase the shared_pool_size parameter to 50M. If the problem persists, then increase the parameter again in increments of 10M until you stop seeing the error.

  • Inadequate Sizing
    The first thing is determining if the ORA-04031 error is a result of lack of contiguous space in the library cache by verifying the following from V$SHARED_POOL_RESERVED:
    REQUEST_FAILURES is > 0 and LAST_FAILURE_SIZE is > SHARED_POOL_RESERVED_MIN_ALLOC
    To resolve this consider increasing SHARED_POOL_RESERVED_MIN_ALLOC to lower the number of objects being cached into the shared pool reserved space and increase SHARED_POOL_RESERVED_SIZE and SHARED_POOL_SIZE to increase the available memory in the shared pool reserved space. 

    Note: a bug was discoverd where LAST_FAILURE_SIZE can be wrong in cases where multiple pools are used. The value in LAST_FAILURE_SIZE can be a sum of failure sizes across all pools. This is filed in unpublished Bug:3669074 and has been fixed as of 9.2.0.7, 10.1.0.4, and 10.2.x.
  • Fragmentation
    If this is not the case, then you must determine if the ORA-04031 was a result of fragmentation in the library cache or in the shared pool reserved space by following this rule:
    REQUEST_FAILURES is > 0 and LAST_FAILURE_SIZE is < SHARED_POOL_RESERVED_MIN_ALLOC.
    If this is the case, consider lowering SHARED_POOL_RESERVED_MIN_ALLOC to allow the database putting more objects into the shared pool reserved space and then increase the SHARED_POOL_SIZE if the problem is not resolved.


Another consideration: pre-9i, changing OPTIMIZER_MAX_PERMUTATIONS to 2000 can reduce shared pool space pressure.

The above checks are still applicable with ORA-04031 in 10g and beyond with auto-tuning functionality. However, explicitly adjusting the setting for SHARED_POOL_SIZE only changes the auto-tuned "minimum" for the Shared Pool. It is a "best practice" to set an explicit setting but to ensure it is low enough to accommodate memory needs in other places if the auto-tuner has to shrink the Shared Pool to handle workload.

3. Resolving error ORA-04031

  • Oracle BUGs

    Oracle recommends to apply the latest patchset available for your platform. Most of the ORA-4031 errors related to BUGs can be avoided by applying these patchsets. See Document 4031.1 with the latest reports of bugs and patches.

    If you run out of memory while compiling a java code (within loadjava or deployejb), you should see an error:

    A SQL exception occurred while compiling: : ORA-04031: unable to allocate bytes of shared memory ("shared pool","unknown object","joxlod: init h", "JOX: ioc_allocate_pal")

    The solution is to shut down the database and set JAVA_POOL_SIZE to a larger value. The mention of "shared pool" in the error message is a misleading reference to running out of memory in the "Shared Global Area". It does not mean you should increase your SHARED_POOL_SIZE. Instead, you must increase your JAVA_POOL_SIZE, restart your server, and try again.

    The cause of this problem has been identified in:
    unpublished Bug:2736601 - CORRECT ORA-4031 MESSAGE TO FLAG JAVA POOL.

  • Small shared pool size

    In many cases, a small shared pool can be the cause of the ORA-04031 error.

    The following information will help you to adjust the size of the shared pool:
    • Library Cache Hit Ratio
      The hit ratio helps to measure the usage of the shared pool based on how many times a SQL/PLSQL statement needed to be parsed instead of being reused. The following SQL statement help you to calculate the library cache hit ratio:
      SELECT SUM(pins) "EXECUTIONS", 
             SUM(reloads) "CACHE MISSES WHILE EXECUTING" 
      FROM v$librarycache;

      If the ratio of misses to executions is more than 1%, then try to reduce the library cache misses by increasing the shared pool size.
    • Shared Pool Size Calculation
      Scripts used to be available for calculating a "best" size for the Shared Pool. Problems arose over time due to changes to internal memory structures and, in some cases, those older scripts could account for certain memory areas twice presenting you with percentages greater than 100%. There are some adjusted scripts available at:
      Document 430473.1 - ORA-4031 Common Analysis/Diagnostic Scripts [Video]

      However, with the introduction of the memory advisors (with 9.2x) and auto-tuning (with 10g Release 2), these estimation scripts are not as useful. See also:
      Document 276103.1 - Performance Tuning Using Advisors and Manageability Features: AWR, ASH, ADDM and Sql Tuning Advisor
  • Shared Pool Fragmentation:

    Every time a SQL or PL/SQL statement needs to be executed the parse representation is loaded in the library cache requiring a specific amount of free contiguous space. The first resource where the database scans is the free memory available in the shared pool. Once the free memory is exhausted, the database looks for reusing an already allocated piece not in use. If a chunk with the exact size is not available, the scan continues looking for space based on the following criteria:
    • The chunk size is larger than the required size
    • The space is contiguous
    • The chunk is available (not in use)

    Then that chunk is split and the remaining free space is added to the appropriate free space list. When the database is operating in this way for a certain period of time the shared pool structure will be fragmented.

    When the shared pool is suffering fragmentation ORA-04031 errors (when the database cannot find a contiguous piece of free memory) may occur. Also as a concequence, the allocation of a piece of free space takes more time an the performance may be affected (the "chunk allocation" is protected by a single latch called "shared pool latch" which is held during the whole operation). However, ORA-4031 errors don't always affect the performance of the database.

    If the SHARED_POOL_SIZE is large enough, most ORA-04031 errors are a result of dynamic SQL fragmenting the shared pool. This can be caused by:
    • Not sharing SQL
    • Making unnecessary parse calls (soft)
    • Setting SESSION_CACHED_CURSORS too high
    • Not using bind variables

    To reduce fragmentation you will need to address one or more of the causes described before. In general to reduce fragmentation you must analyze how the application is using the shared pool and maximize the use of sharable cursors.

    Please refer to Document 62143.1, which describes these options in greater detail. This note contains as well further detail on how the shared pool works. The following views will help you to identify non-sharable versions of SQL/PLSQL text in the shared pool:
    • V$SQLAREA View
      This view keeps information of every SQL statement and PL/SQL block executed in the database. The following SQL can show you statements with literal values or candidates to include bind variables:
      SELECT SUBSTR(sql_text,1,40) "SQL", 
             COUNT(*), 
             SUM(executions) "TotExecs" 
      FROM v$sqlarea 
      WHERE executions < 5 
      GROUP BY SUBSTR(sql_text,1,40) 
      HAVING count(*) > 30 
      ORDER BY 2;

      Note: The number "30" in the having section of the statement can be adjusted as needed to get more detailed information.
    • X$KSMLRU View

      There is a fixed table called X$KSMLRU that tracks allocations in the shared pool that cause other objects in the shared pool to be aged out. This fixed table can be used to identify what is causing the large allocation.

      If many objects are being periodically flushed from the shared pool then this will cause response time problems and will likely cause library cache latch contention problems when the objects are reloaded into the shared pool.

      One unusual thing about the X$KSMLRU fixed table is that the contents of the fixed table are erased whenever someone selects from the fixed table. This is done since the fixed table stores only the largest allocations that have occurred. The values are reset after being selected so that subsequent large allocations can be noted even if they were not quite as large as others that occurred previously. Because of this resetting, the output of selecting from this table should be carefully kept since it cannot be retrieved back after the query is issued.

      To monitor this fixed table just run the following:
      SELECT *
      FROM X$KSMLRU
      WHERE ksmlrsiz > 0;

      This view can only be queried by connected as the SYS.
    • X$KSMSP View (Similar to Heapdump Information)

      Using this view you will be able to find out how the free space is currently allocated, which will be helpful to undrestand the level of fragmentation of the shared pool. As it was described before, the first place to find a chunck big enough for the cursor allocation is the free list. The following SQL shows the chunks available in the free list:
      SELECT '0 (<140)' bucket, ksmchcls, ksmchidx, 10*TRUNC(ksmchsiz/10) "From",
             COUNT(*) "Count", MAX(ksmchsiz) "Biggest",
             TRUNC(AVG(ksmchsiz)) "AvgSize", TRUNC(SUM(ksmchsiz)) "Total" 
      FROM x$ksmsp 
      WHERE ksmchsiz<140 
      AND ksmchcls='free' 
      GROUP BY ksmchcls, ksmchidx, 10*TRUNC(ksmchsiz/10)
      UNION ALL
      SELECT '1 (140-267)' bucket, ksmchcls, ksmchidx,20*TRUNC(ksmchsiz/20), 
             COUNT(*), MAX(ksmchsiz), 
             TRUNC(AVG(ksmchsiz)) "AvgSize", TRUNC(SUM(ksmchsiz)) "Total" 
      FROM x$ksmsp 
      WHERE ksmchsiz BETWEEN 140 AND 267 
      AND ksmchcls='free' 
      GROUP BY ksmchcls, ksmchidx, 20*TRUNC(ksmchsiz/20) 
      UNION ALL 
      SELECT '2 (268-523)' bucket, ksmchcls, ksmchidx, 50*TRUNC(ksmchsiz/50), 
             COUNT(*), MAX(ksmchsiz), 
             TRUNC(AVG(ksmchsiz)) "AvgSize", TRUNC(SUM(ksmchsiz)) "Total" 
      FROM x$ksmsp 
      WHERE ksmchsiz BETWEEN 268 AND 523 
      AND ksmchcls='free' 
      GROUP BY ksmchcls, ksmchidx, 50*TRUNC(ksmchsiz/50) 
      UNION ALL 
      SELECT '3-5 (524-4107)' bucket, ksmchcls, ksmchidx, 500*TRUNC(ksmchsiz/500), 
             COUNT(*), MAX(ksmchsiz) ,
             TRUNC(AVG(ksmchsiz)) "AvgSize", TRUNC(SUM(ksmchsiz)) "Total" 
      FROM x$ksmsp 
      WHERE ksmchsiz BETWEEN 524 AND 4107 
      AND ksmchcls='free' 
      GROUP BY ksmchcls, ksmchidx, 500*TRUNC(ksmchsiz/500) 
      UNION ALL 
      SELECT '6+ (4108+)' bucket, ksmchcls, ksmchidx, 1000*TRUNC(ksmchsiz/1000), 
             COUNT(*), MAX(ksmchsiz), 
             TRUNC(AVG(ksmchsiz)) "AvgSize", TRUNC(SUM(ksmchsiz)) "Total" 
      FROM x$ksmsp 
      WHERE ksmchsiz >= 4108 
      AND ksmchcls='free' 
      GROUP BY ksmchcls, ksmchidx, 1000*TRUNC(ksmchsiz/1000);

      Note: The information available in this view is the same that is generated as part of a HEAPDUMP level 2. 

      Be aware of  Bug:4715420 stating that selecting from X$KSMSP is asking one session to hold the shared pool latches in turn for a LONG period of time and should be avoided on live systems. Selecting from X$KSMSP on a production system is a very bad idea.

      If the result of the above query shows that must of the space available is on the top part of the list (meaning available only in very small chuncks). It is very likely that the error is due to a heavy fragmentation. 

      You can also use this view as follows to review overall memory usage in the SGA:
      SQL> SELECT ksmchcls CLASS, COUNT(ksmchcls) NUM, SUM(ksmchsiz) SIZ,
      TO_CHAR(((SUM(ksmchsiz)/COUNT(ksmchcls)/1024)),'999,999.00')||'k' "AVG SIZE" 
      FROM X$KSMSP GROUP BY ksmchcls; 

      CLASS           NUM        SIZ AVG SIZE
      -------- ---------- ---------- ------------
      R-free           12    8059200      655.86k
      R-freea          24        960         .04k
      free            331  151736448      447.67k
      freeabl        4768    7514504        1.54k
      perm              2   30765848   15,022.39k
      recr           3577    3248864         .89k

      1. if free memory (SIZ) is low (less than 5mb or so) you may need to increase the shared_pool_size and shared_pool_reserved_size.
      2. if perm continually grows then it is possible you are seeing system memory leak.
      3. if freeabl and recr are always huge, this indicates that you have lots of cursor info stored that is not releasing.
      4. if free is huge but you are still getting 4031 errors, (you can correlate that with the reloads and invalids causing fragmentation).

4. ORA-04031 error and Large Pool

The Large pool is an optional memory area that can be configured to provide large memory allocations for one of the following operations:

  • session memory for the multi-threaded server and the Oracle XA interface.
  • The memory ( Buffers ) for Oracle backup and restore operations and for I/O server processes.
  • Parallel Execution messaging buffers.

The Large pool does not have a LRU list. It is different from reserved space in the shared pool, which uses the same LRU list as other memory allocated from the shared pool. Chunks of memory are never aged out of the large pool, memory has to be explicitly allocated and freed by each session. If there is no free memory left when a request is made then an ORA-4031 will be signalled similar to this:

ORA-04031: unable to allocate XXXX bytes of shared memory ("large pool","unknown object","session heap","frame")


Few things can be checked when this error occurs:

  1. Check V$SGASTAT and see how much memory is used and free using the following SQL statement:
    SELECT pool, name, bytes
    FROM v$sgastat
    WHERE pool = 'large pool';
  2. You can also take a heapdump level 32 to dump the large pool heap and check free chunks sizes.


Memory is allocated from the large pool in chunks of LARGE_POOL_MIN_ALLOC bytes to help avoid fragmentation. Any request to allocate a chunk size less LARGE_POOL_MIN_ALLOC will be allocated with size of LARGE_POOL_MIN_ALLOC. In general you may see more memory usage when using Large Pool compared to Shared Pool.

Usually to resolve an ORA-4031 in the large pool the LARGE_POOL_SIZE size must be increased.

5. ORA-04031 and SHARED POOL FLUSHING

There are several techniques to increase cursor sharability so that shared pool fragmentation is reduce as well as likeability of ORA-4031 errors. The best way is by modifying the application to use bind variables. Another workaround when the application cannot be modified is using CURSOR_SHARING to a value different of EXACT (Be aware that this may cause changes in execution plan, so it is advisable to test the application first). When none of the above techniques can be used and fragmentation is considearble heavy in the system, flushing the shared pool might help alliviating the fragmentation. However some considerations must be taken into account:

  • Flushing the shared pool will cause that all the cursor that are not in use are removed from the library cache. Therefore just after the shared pool flusing is issued, most of the SQL and PL/SQL cursors will have to be hard parsed. This will increase the CPU usage of the system and will also increase the latch activity.
  • When applications don't use bind variables and have heavy possibilities of many users doing frequen similar operations (like in OLTP systems) it is common that soon after the flush is issued the fragmentation is back in place. So be advice that flushing the shared pool is not always the solution for a bad application.
  • For large shared pool flushing the shared pool may cause a halt of the system, specially when the instance is very active. It is recommended to flush the shared pool during off-peak hours.

6. Advanced analysis to ORA-04031 error

If none of the techniques provided cannot resolve the occurence of ORA-04031 errors, additional tracing may be needed to get a snapshot of the shared pool when the problem is in place.

Modify the init.ora paramater to add the following events to get a trace file with additional information about the problem:

event = "4031 trace name errorstack level 3"
event = "4031 trace name HEAPDUMP level 2"

 

Note: These parameters will take not effect unless the instance is bounced.


Starting with 9.2.0.5, instead of requesting heapdump level 1,2, 3 or 32 you can use level those same levels plus (536870912). This will generate the 5 largest subheaps AND the 5 largest heap areas within each of those. If the problem is reproducible, the event can be set at session level using the following statement before the execution of the faulty SQL statement:

SQL> ALTER SESSION SET EVENTS '4031 trace name errorstack level 3';
SQL> ALTER SESSION SET EVENTS '4031 trace name heapdump level 536870914';


The trace file(s) should be sent to Oracle Support for troubleshooting.

Important Note: In Oracle 9.2.0.5 and higher releases a trace file is generated BY DEFAULT every time an ORA-4031 error occurs, and can be located in the USER_DUMP_DEST directory (or ADR with 11g). If your database version is one of these, you don't need to follow the steps described before to generate additional tracing.

 

Community Discussion

For further discussion or questions about topics in this article, please visit 'Diagnosing and Resolving ORA-4031 errors'.

REFERENCES


NOTE:1012046.6 - How to Calculate Your Shared Pool Size

NOTE:316138.1 - ORA-4031 / Continuous Growth of 'miscellaneous' in v$sgastat when STATISTICS_LEVEL is set to TYPICAL or ALL
NOTE:367392.1 - ORA-4031 with calls to ksfd_alloc_sgabuffer, ksfd_alloc_contig_buffer, ksfd_get_contig_buffer
NOTE:396940.1 - Troubleshooting and Diagnosing ORA-4031 Error [Video]
NOTE:61623.1 - Resolving Shared Pool Fragmentation In Oracle7
NOTE:62143.1 - Troubleshooting: Tuning the Shared Pool and Tuning Library Cache Latch Contention
NOTE:4031.1 - OERR: ORA-4031 "unable to allocate %s bytes of shared memory ("%s","%s","%s")"
 
 

附件

   
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值