|
In this Document
Applies to:Oracle Database - Enterprise Edition - Version 8.1.5.0 and laterInformation in this document applies to any platform. PurposeThis article is intended to
Last updated June 2010
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. Common Bugs
Review Note: 4031.1 for latest bug information searchable by release level. Questions and AnswersHow is memory allocated and deallocated in the SGA pools?The SGA is comprised of fixed areas like the Log Buffers and the Buffer Cache(s) as well as memory pools (Shared Pool, Large Pool, Java Pool, and in 10g, the Streams Pool). Memory allocated to the various pools are divided in heaps that can be composed of one or many sub heap memory structures.
This is similar to many segments
inside a tablespace. The tablespace will be the heap and the segments
are the subheaps. The extents within the segment are like the various
subheaps that can exist inside "parent" subheaps. The goal in the
shared memory areas is the same as a tablespace--avoid fragmentation.
To do this we allocate the chunk of memory that best fits the request
for memory, coalescing adjacent free space as needed and detecting
memory that can be flushed and reused. The Shared Pool and Large Pool divide their shared memory areas into subpools (starting with 9i). Each subpool will have Free List Buckets containing pointers to memory chunks within the subpool. The other pools are treated as one large memory area with a single Free List as described above.
When a memory chunk is allocated inside the memory pool, it
will be associated with a memory type. The chunk will be allocated as
PERMANENT, FREEABLE, or RECREATABLE. These memory chunks are then
associated with a memory structure or element inside the pool. For
example, "KGLS heap". These memory structures/elements are not always
tracked/commented in the data dictionary (specifically those in the
PERMANENT type). What are Subpools?
In Oracle 9i and later versions, the Shared Pool can be divided into
subpools. Each subpool is a "mini" shared pool, having its own set of
Free Lists, memory structure entries, and LRU list. This was a
scalability change made to the Shared Pool/Large Pool to increase the
throughput of these pools in that now each subpool is protected by
a Pool child latch. This means there is no longer contention in the
Shared/Large Pool for a single latch as in earlier versions. The
reserved area for the Shared Pool is divided equally throughout the
subpools as well. What is the Reserved Area?
A cache miss on the data dictionary cache or library cache is more
expensive than a miss on the buffer cache. For this reason, the Shared
Pool should be sized to ensure that frequently used data is cached. If
there is not enough free space in the Shared Pool, then Oracle must
search for and free enough memory to satisfy this request. This
operation could conceivably hold latch resources for detectable periods
of time, causing minor disruption to other concurrent attempts at memory
allocation. Missed space requests for large size allocations can thus
be very costly since it may cause many smaller pieces of pool memory
By default, Oracle configures a small Reserved Pool (or Reserved
Area) inside the Shared Pool. This memory can be used to satisify large
contiguous allocation requests when space is not available in the
general shared pool list.
In some application environments, 5% is too small. If an ORA-04031 problem indicates a very large memory request failure, increase the size of the Reserved Area by manually setting SHARED_POOL_RESERVED_SIZE or if using SGA_TARGET>0, change the hidden parameter, _shared_pool_reserved_pct to 10 or 15 (see example below) to ensure that when the Shared Pool grows or shrinks automatically, the Reserved Area will change as well.
The view V$SHARED_POOL_RESERVED can be used to determine failed request sizes and shared pool reserved area utilization. Document: 430473.1 ORA-4031 Common Analysis/Diagnostic Scripts contains a script for querying this view.
SQL> alter system set "_shared_pool_reserved_pct"=10 scope=spfile
or add this in the pfile
"_shared_pool_reserved_pct"=10
For large allocations, Oracle attempts to allocate space in the Shared Pool in the following order:
1. From the unreserved part of the shared pool. What are the pools in the SGA used for?The Shared Pool portion of the SGA contains the library cache, the dictionary cache, buffers for parallel execution messages, and control structures. The library cache includes the shared SQL areas, private SQL areas (in the case of a shared server configuration), PL/SQL procedures and packages, and control structures such as locks and library cache handles. We allocate memory from the shared pool when a new SQL statement is parsed, to store in the shared SQL area. The size of this memory depends on the complexity of the statement. Ideally, the Shared Pool should be used for caching shared SQL and to avoid the performance overhead caused by shrinking the shared SQL cache. Many features of Oracle like Recovery Manager (RMAN), parallel processing/IO slave processing, and Shared Server are designed to utilize large shared memory chunks . These features will put unnecessary stress on the Shared Pool and therefore we recommend you define a Large Pool using LARGE_POOL_SIZE or by using SGA_TARGET to help reduce memory stress in the Shared Pool in these scenarios. The Java Pool memory is used for memory allocations associated with all session-specific Java code and data within the JVM. Java pool memory is used in different ways, depending on what mode the Oracle server is running in. If using Streams functionality, you can configure the Streams Pool to manage memory allocations needed for this functionality. The Shared Pool employs a LRU algorithm similar to what is found in the Buffer Cache. Therefore, tuning the Shared Pool is more complex than other pools. Most of the time, if an ORA-04031 errors occur in one of the other memory pools, this indicates that the pool is too small and you must increase the size of the problem pool to stop these errors in the future. The default settings for these other pools are usually sufficient, but to manually adjust these pools, you can alter the parameters LARGE_POOL_SIZE, STREAMS_POOL_SIZE, and JAVA_POOL_SIZE. Using SGA_TARGET these pools are automatically adjusted as needed by the MMAN process. What is an ORA-04031 Error?
The memory pool in the SGA are
comprised of memory chunks in various sizes. When the database starts is
started, you have a large chunk of memory allocated in the various
pools and tracked in free list hash buckets. Over time, as memory is
allocated and deallocated, the memory chunks are moved around into
different free list buckets inside the pool according to their size. An
ORA-04031 error occurs in any of the memory pools in the SGA when Oracle
cannot find a memory chunk large enough to satisfy an internal
allocation request on behalf of a user's operation.
scan regular free list for match, if not found NOTE: The Shared Pool is used in an ASM environment as well. There have been reports of ORA-04031 on 10.1. x ASM instances because the default size can be too small to accommodate the diskgroup management activities. In these cases, set the SHARED_POOL_SIZE parameter to 50M and increase the setting in increments of 10M if the problems persist. Note 146599.1 Diagnosing and Resolving Error ORA-04031 Is my Reserved Area sized properly?An ORA-04031 error referencing a large failed requests indicates the Reserved Area is too fragmented. You can investigate memory usage in the reserved area using the script from Note 430473.1
ReservedAnalysis.sql
Request Misses = 0 can mean the Reserved Area is too big. Request Misses always increasing but Request Failures not increasing can mean the Reserved Area is too small. In this case flushes in the Shared Pool satisfied the memory needs. Request Misses and Request Failures always increasing can mean the Reserved Area is too small and flushes in the Shared Pool are not helping (likely got an ORA-04031). You can also investigate the efficiency of the size of your Reserved Area. The goal is to have the "Hit %" stay as close to 100 as possible. NOTE: Failures in the Reserved Area do not always equate to ORA-04031 errors. We perform mini-flushes to try to find matching memory requests and in many cases we will find the requested memory and avert the error message. If you increase the size of the Reserved Area, you can increase the chances of taking needed memory from the Shared Pool. We recommend you increase the Shared Pool and the Reserved Area sizes by the same amount.
col requests for 999,999,999
col last_failure_size for 999,999,999 head "LAST FAILURE| SIZE " col last_miss_size for 999,999,999 head "LAST MISS|SIZE " col pct for 999 head "HIT|% " col request_failures for 999,999,999,999 head "FAILURES" select requests, decode(requests,0,0,trunc(100-(100*(request_misses/requests)),0)) PCT, request_failures, last_miss_size, last_failure_size from v$shared_pool_reserved;
Is there a way to find a "right" size for the Shared Pool?You can configure the Shared Pool manually using the SHARED_POOL_SIZE parameter or have the pool auto-tuned using SGA_TARGET (10g and higher). Part of the memory allocated for the Shared Pool is overhead memory (based on settings for some internal parameters). Prior to 10g, this overhead memory was "on top" of the SHARED_POOL_SIZE parameter setting, but not reflected by the parameter SHARED_POOL_SIZE. This appears to be a calculation error when you run a query on V$SGASTAT prior to 10G.
Example,
SHARED_POOL_SIZE=64M Overhead=12M SQL> Select sum(bytes) "Total Mem" from v$sgastat where pool='shared pool'; Total Mem ----------- 79691776 With 10g, this overhead memory is now included inside the SHARED_POOL_SIZE setting. Some customers run into memory issues in the Shared Pool after a move to 10g if they do not account for this overhead memory. From the example above, if the SHARED_POOL_SIZE is manually set to 64M and the overhead remains unchanged, this means the usable Shared Pool memory is only 54525952 bytes.
NOTE: Scripts like in the Metalink article 105813.1 SCRIPT TO SUGGEST
MINIMUM SHARED POOL SIZE. are useful in older releases of the
database, but do not work consistently with Oracle 9.2.x and higher. For
other pointers on Shared Pool sizing in 10g, see How much free memory is available in my SGA?You can see the free memory for the Shared Pool in the view V$SGASTAT. The view is broken down into memory structure table entries like 'library cache', 'KGLS heap', 'CCursor'. Prior to 10g, there were only a handful of table entries tracked in this view so small memory structures were lumped together in the table entry 'miscellaneous'.
What is managed automatically through 10g ASMM and/or 11g AMM?The Automatic Shared Memory Management functionality is "turned on" when SGA_TARGET>0 on Release 10g. ASMM will manage the "best" size for these components in the SGA
Memory is moved in "granule sized" chunks. You can find the granule size for your database querying V$SGAINFO. Refer to Note 947152.1 for more on problems with large granule sizes. Very large SGAs will use very large granule sizes by default.
The other buffer caches (managed through parameters
DB_nK_CACHE_SIZE, DB_KEEP_CACHE_SIZE, DB_RECYCLE_CACHE_SIZE), Log
Buffer, and Fixed SGA areas are not automatically tuned by MMAN.
However these settings do affect the actual memory available to MMAN in
the SGA_TARGET setting.
Case Study: Per the configuration above, this memory has to be part of the SGA:
Log Buffers 209,715,200 There are benefits to manually setting a minimum size for the auto-tuned components of the SGA, but it does have an impact on the amount of memory that MMAN has access to when needing to grow and shrink various components. We DO recommend setting default/explicit settings for the auto-tuned components in the SGA. To Gauge how ASMM is working, issue the script from Note 430473.1
SGAComponents.sql (for 10.2.x)
SGAComponents11g.sql (for 11g)
A rule of thumb for setting up ASMM is
1. Observe trends and find the right values for a minimum size for the various pools from the scripts above.
Warning: There have been a number of issues filed
on 10.2.x with ASMM and ORA-4031. Many customers do not set minimum
sizes for the various auto-tuned pools instead relying on sga_target and
MMAN to move memory around as is needed. Internal tests and from
discussions with development, it is better to find a minimum setting for
these pool and manually set that in the spfile or init parameter
file. As a starting point, review data in v$sga_dynamic_components and
manually assign values to the pools at 70 or 75% of the current_size.
If there are indications of failed attempts to shrink the shared pool
below that number over time, then decrease the default setting by
another 10% and monitor that the new value is a better minimum
setting. This will help with decreasing chances of seeing excessive
pinging of memory between the buffer cache and the various pools. NOTE: With 11g, IMMEDIATE memory requests to the pools in the SGA can be moved around in the SGA even when MEMORY_TARGET and SGA_TARGET are explicitly set to 0. See Note 1269139.1
How many Subpools will I have by default?
The number of subpools is
calculated using a simple algorithm. First, a subpool must be at least
128MB in 9i releases and at least 256MB in 10g releases. Second, there
can be one subpool for every four CPUs on the system, up to 7 subpools.
The number of subpools can be explicitly controlled using the init.ora
parameter _kghdsidx_count. There is no parameter to explicitly control
the size of each subpool.
Because 128MB (and even 256MB on
10g) subpools can be small in many application environments, the memory
per subpool will likely need to be increased. There is no parameter to
change the minimum size of the subpool; the only recourse is to
decrease the number of subpools for a given shared pool size or increase
the shared pool size so the size of the subpools increase. Please
remember that increasing the size of the shared pool does not
necessarily increase the size of the subpool, because the number of
subpools can increase if there are many CPUs on the system. How do I control the number of subpools used?
The parameter _kghdsidx_count controls the number of subpools used. Setting the value of this parameter to one "reverts" the shared pool behavior back to 8.1.7 behavior, e.g., one subpool.
SQL> alter system set "_kghdsidx_count"=1scope=spfile;
or add this in the pfile
"_kghdsidx_count"=1
NOTE: Subpools are created at startup when the SGA is created. In both examples above, the database has to be restarted to change the number of subpools created. Any change to the _kghdsidx_count will change the number of subpools in the Large Pool as well.
Warning: Reducing the number of subpools may have a noticable impact on performance, especially
on RAC configurations, highly concurrent systems, or database instance with very large pools. Altering this parameter will affect the Shared pool, Shared pool reserved and Large pool. Reducing the number of subpools could result in increased latch contention in the pool area. When setting the number of subpools manually with _kghdsidx_count, recommend making the changes incrementally to monitor performance impact and minimize any drastic impact. Conversely, increasing number of subpools without increasing overall pool size could lead to space issues since subpools could be undersized.
Are all ORA-04031 errors reported in the alert log?
No. Some errors only show up at the client workstation. Prior to
11g, any ORA-4031 error that occurs on a regular user process will not
be documented in the alert log. There can also be cases where an
ORA-600 or ORA-7445 error is reported in the alert log but the internal
error was a side-effect of a 4031 memory issue. The associated trace
for the internal error may contain diagnostic data usually included with
a default ORA-4031 trace.
How can we see a breakdown of the data in the "miscellaneous" structure in V$SGASTAT?When running a query on v$sgastat, there are cases where you will see a very large value for "miscellaneous". Until Oracle 10g Release 2, the basic design of the SGA structure internally remained unchanged. There were table entries for various memory "comments" in the data dictionary where we keep statistics on the largest memory "comments" or structures. The smaller structures are lumped together in the memory "comment" called miscellaneous because we tracked only a handful of structures. If you run a script like SGAStat.sql from Note: 430473.1. This script will report back the biggest allocation areas in the view V$SGASTAT for the Shared Pool. You can adjust the script to look at any of the Pools in the SGA. Per bug 3663344, there were occasional inconsistencies in the statistics reported in V$SGASTAT due to memory structures growing and shrinking over time. Once a memory structure reached an internally controlled size, we moved the data about the memory structure from the general purpose table entry, "miscellaneous", to a specific memory structure table entry. The problem was usually seen with negative numbers in some memory structures in v$sgastat or at times you would see an unusually large value in "miscellaneous". The only way to "drill-down" into the way memory is allocated in the "miscellaneous" area is to get a heapdump trace. You can issue this command
alter system set events '4031 trace name HEAPDUMP level 536870914';
NOTE: Setting this event at the instance level will generate large files and if the 4031 error occurs frequently, you will get many trace files. This can impact performance and hang (and in some cases crash a database). Turn this event off using alter system set events '4031 trace name HEAPDUMP off'; and at the next occurrences of the 4031 problem you will get a breakdown of the memory in the SGA and also the breakdown of memory used in the top five subheaps in the SGA. In this scenario, you would expect one or more of the largest subheaps listed in this trace to be within 'miscellaneous'. Unfortunately, there isn't a way to see the entire breakdown within 'miscellaneous', but we only need to be concerned about larger than expected entries within 'miscellaneous'. To get an immediate memory dump use these steps
alter system set events 'immediate trace name heapdump level 536870914'; Close the SQL*Plus session and find the heapdump trace file listed in the 'oradebug tracefile_name' command above. If the problem is actually associated with permanent memory structures (tracked under the 'miscellaneous' table entry), there is not a way to get information on these memory areas unless you set the event 10235 level 65536. This event should only be set under direction from Oracle Support.
What database parameters are relevant to ORA-04031 problems?
Literal replacement is
a feature where Oracle replaces literal values in SQL statements to
reduce the application Shared Pool footprint and decrease "hard"
parsing. The literal values are replaced with bind variables and if two
or more sessions are executing the same SQL statement, they can both
use the same cursor with the bind variable instead of creating two
unsharable cursors.
Invalidations are caused by either executing DDL against the objects, gathering stats, or granting/revoking privileges. You should see associated "library cache pin" waits also.
References:
The implementation of
SIMILAR is not optimal. Even though cursors with literal will have the
best plans, the match criteria is based on the values of the literals.
The implementation creates a mini-hash table based on literal values,
and if the values are the same, the child cursors are shared. This
behavior can lead to an application's Shared Pool footprint equivalent
to running with CURSOR_SHARING=EXACT. Instead of creating lots of
individual cursors with their own children, SIMILAR creates lots of
child cursors under the same parent cursor object. If you see hard
parses at 50/sec or higher (Snapshot/AWR), CURSOR_SHARING=EXACT can be
helpful.
If this parameter is
set, Oracle does not unpin the library cache object at the end of
execute. This means that the active amount of memory pinned within the
library cache increases as more cursors are opened and executed,
reducing the amount of memory that can be aged out of the Shared Pool.
This parameter must be used carefully and with knowledge about the
total application footprint within the Shared Pool. Setting of this
parameter without this knowledge can lead to ORA-04031 errors. Cursors
that are on the Session Cached Cursor list do not have their SQL area
heaps pinned.
Review the size set for the parameter in the RDA. If using SGA_TARGET, this will default to 0. Any hard-coded setting for this parameter when using SGA_TARGET will act as a minimum size for MMAN when it attempts to shrink the Buffer Cache.
This SGA memory component is not auto-tuned, but the memory setting will affect the actual memory available to MMAN.
This SGA memory component is not auto-tuned, but the memory setting will affect the actual memory available to MMAN.
This SGA memory component is not auto-tuned, but the memory setting will affect the actual memory available to MMAN.
If using SGA_TARGET, this will default to 0. Any hard-coded setting for this parameter when using SGA_TARGET will act as a minimum size for MMAN when it attempts to shrink the Java Pool. If you are not using SGA_TARGET, the default size for this memory area is usually sufficient unless your environment utilizes a lot of JVM objects.
If you need to see more detail on how the memory is allocated in the Java Pool, you can issue
> sqlplus /nolog
SQL> connect / as sysdba SQL> alter session set events 'immediate trace name heapdump level 128';
The Large Pool is
intended to offload larger memory allocations from the Shared Pool
related to Shared Server (UGA), Parallel Processing (Buffer
allocations), and RMAN backup operations, and sequential file IO (e.g.
IO slave activity). If you are not using these specific functionality
areas, you can set the Large Pool to 0. The Large Pool does not use a
Least Recently Used (LRU) algorithm, so until a session releases larger
memory allocations, they remain in the Large Pool.
> sqlplus /nolog
SQL> connect / as sysdba SQL> alter session set events 'immediate trace name heapdump level 32'; If using SGA_TARGET, the parameter will show as 0, but you can hard-code a minimum size and MMAN will not attempt to shrink the Large Pool below that setting.
This SGA memory component is not auto-tuned, but the memory setting will affect the actual memory available to MMAN.
This parameter sets
the upper bound for the number of cursor that a session can have open.
Normally, cursors are opened through an OCI call or through a PL/SQL
call to open a cursor.
These processes will impact the size of the shared pool starting in 9.2.x. Memory structures are located in the Shared Pool with 9.2.x to store dynamic parameter setting information per session/process. NOTE: The memory structure can take as much as 20 bytes per parameter for 32-bit databases and 32 bytes per parameter for 64-bit databases. At 10.2.x, there are over 1300 dynamic parameters, so this can add up quickly with a lot of users on the database. You can review the high water mark for Sessions and Processes in the V$RESOURCE_LIMIT view. If the hard-coded values for these parameters are much higher than the high water mark information, consider decreasing the parameter settings to free up some memory in the Shared Pool for other uses.
When a cursor is
closed, Oracle divorces all association between the session and the
library cache state. If no other session has the same cursor opened, the
library cache object and its heaps are unpinned and available for an
LRU operation. The parameter SESSION_CACHED_CURSORS controls the number
of cursors "soft" closed, much like the cached PL/SQL cursors.
If this parameter is set, the MMAN process will attempt to grow and shrink auto-tuned memory components. Interestingly, on 10.2, if you specify in the spfile an explicit setting for SGA_TARGET higher than the explicit value for SGA_MAX_SIZE the next startup will ignore the prior setting for SGA_MAX_SIZE and set it equal to the new SGA_TARGET setting. This is not the behavior at 11g.
If using SGA_TARGET, this will default to 0, but a hard-coded setting for this parameter will act as a minimum size for MMAN when it attempts to shrink the Shared Pool. With 9i and 10g, more SGA fixed memory structures have been moved to the Shared Pool. This means that when upgrading from Oracle7 and Oracle8/Oracle8i, you must perform additional tuning analysis on the 9i or 10g memory needs for the Shared Pool. With 9i and higher, Oracle also implemented a new subpool functionality. This can require additional tuning analysis as the application(s) will utilize memory differently. In some cases too many subpools inside the Shared Pool, can cause one of the subpools to be over-utilized and lead to ORA-4031 problems. Reference: Note 270935.1 Shared pool sizing in 10g If you need to see more detail on how the memory is allocated in the Shared Pool, you can issue
> sqlplus /nolog
SQL> connect / as sysdba SQL> alter session set events 'immediate trace name heapdump level 2'; NOTE: Running the heapdump trace is not recommended during peak activity on the database. The tracing will affect performance.
This parameter defaults to 5% of the SHARED_POOL_SIZE setting. When using SGA_TARGET, this will be adjusted as the Shared Pool component grows and shrinks automatically. If you are seeing consistent ORA-4031 errors that indicate memory request failures larger than 4000 bytes, the 5% default value may not be sufficient in your application environment. You can change the hidden parameter, _shared_pool_reserved_pct, to 10. This will cause the Reserved Area to utilize 10% of the Shared Pool. For example,
SQL> alter system set "_shared_pool_reserved_pct"=10 scope=spfile;
or add in the init file "_shared_pool_reserved_pct"=10
There are additional memory structures in 10g related to Statistics tracking. This parameter controls all major statistics collections or advisories in the database and sets the statistics collection level for the database. The parameter can be set to BASIC, TYPICAL, or ALL.
The default setting of
TYPICAL will put strain on the Shared Pool unless you tune the Shared
Pool to accommodate the ongoing analysis activity in the database. In
some performance tuning cases, it is necessary to set STATISTICS_LEVEL
to ALL. This will use more memory in the Shared Pool than the other
settings, so using ALL can cause ORA-4031 problems if the Shared Pool is
not tuned to handle the additional memory needs.
This is new memory pool in 10g. It is intended to alleviate stress on memory structures in the Shared Pool related to Streams operations. Review the size set for the parameter in the RDA. If using SGA_TARGET on 10g Release 2, the parameter will be auto-tuned and will show up as 0. You can hard-code a minimum size with 10g and Release 2 and MMAN will not attempt to shrink the Streams Pool below that setting. If you need to see more detail on how the memory is allocated in the Java Pool, you can issue
> sqlplus /nolog
SQL> connect / as sysdba SQL> alter session set events 'immediate trace name heapdump level 64';
Also review any settings that would indicate the use of Shared Server (MTS_SERVERS, MTS_DISPATCHERS, etc). If these parameters indicate that Shared Server is configured, you should not see memory structures in the Shared Pool related to Shared Server NOTE: Some of the parameters listed above are only applicable if using SGA_TARGET. Be sure to investigate exactly how much memory MMAN can work with to grow and shrink the auto-tuned memory components. What should we look at in an RDA to help diagnose a 4031 error?Review the RDA report because many of the "pieces of the puzzle" or in a single report. However, unless the problem is a simple case of the memory component not configured or too small, other diagnostic information is often needed to find the root issue: Under 'Overview', 'System Information', review the number of CPUs on this server. The number of CPUs helps determine the number of subpools used in the Shared Pool. For example,
Under 'Overview', 'Database Information', review the release level information (V$VERSION)
Under 'RDBMS', 'Database Parameters', review the appropriate parameters as described above. With the latest versions of the RDA report, the hidden parameter settings are included in this section. You should review the settings for '_PX_use_large_pool', '_kghdsidx_count', '_large_pool_min_alloc', '_library_cache_advice', '_shared_pool_reserved_pct', '_shared_pool_reserved_min_alloc', '_4031_dump_bitvec', '_4031_max_dumps', '_4031_dump_interval', '_4031_sga_dump_interval', '_4031_sga_max_dumps'.
Under 'RDBMS', 'V$RESOURCE_LIMIT', review the high water mark information for 'processes', 'sessions'. If the settings for PROCESSES and SESSIONS is much higher than high water mark numbers, decreasing these settings could help decrease some permanent memory structure allocations. In RAC environments, the parameter settings for 'ges*' parameters can also be important. There are some RAC/ORA-04031 bugs related to the 'ges%' parameters.
What is relevant in the default 4031 trace file?
With 9.2.0.5 and higher, a trace file is generated at the time of an
ORA-04031 error (controlled by _4031_dump_bitvec). On 9.2.x, the
default: is 6639615 = 0x654fff , which means:
*** SESSION ID:(242.24755) 2006-08-29 08:55:15.765
================================= Begin 4031 Diagnostic Information ================================= The following information assists Oracle in diagnosing causes of ORA-4031 errors. This trace may be disabled by setting the init.ora parameter _4031_dump_bitvec = 0 ====================================== Allocation Request Summary Information ====================================== Current information setting: 00654fff Dump Interval=300 seconds SGA Heap Dump Interval=3600 seconds Last Dump Time=08/29/2006 08:55:14 Allocation request for: optdef : apanlg <<<< request for memeory structure (related to inlist and the CBO) Heap: 44b5c89b8, size: 96 <<<****************************************************** ****************************************************** HEAP DUMP heap name="sga heap" desc=380000030 extent sz=0xfe0 alt=200 het=32767 rec=9 flg=-126 opc=0 parent=0 owner=0 nex=0 xsz=0x1 ****************************************************** HEAP DUMP heap name="sql area" desc=44b5c89b8 < in "sql area" extent sz=0x1040 alt=32767 het=32 rec=0 flg=2 opc=2 parent=380000030 owner=44b5c8898 nex=0 xsz=0x1 Subheap has 840329704 bytes of memory allocated Scroll down the trace file until just after the Stack Trace listing,
----- End of Call Stack Trace -----
=============================== Memory Utilization of Subpool 1 <=============================== Allocation Name Size _________________________ __________ "free memory " 124944864 <<<< this was "free memory" at time of error "miscellaneous " 57893552 <<<< prior to 10gR2, this is a general purpose holder for a lot of smaller memory areas "sim memory hea " 2319640 "PL/SQL PPCODE " 0 "KQR L SO " 56320 "type object de " 0 "trigger source " 0 "errors " 0 "PX subheap " 147016 "trigger defini " 0 "trigger inform " 0 "PLS non-lib hp " 2088 "KGLS heap " 215352 "FileOpenBlock " 7517528 "KQR M SO " 39976 "PL/SQL SOURCE " 0 "PL/SQL DIANA " 99968 "joxlod: in phe " 0 "db_block_hash_buckets " 9978352 "joxs heap init " 4240 "MTTR advisory " 697248 "fixed allocation callback" 552 "dictionary cache " 3229952 "KQR L PO " 245784 "KQR M PO " 319096 "parameters " 0 "partitioning d " 0 "library cache " 18615496 "table definiti " 0 "sql area " 901605416 < of memory already allocated here "pl/sql source " 0 "transaction co " 0 "KGK heap " 7000 "KQR S SO " 14360 "event statistics per sess" 12499760 "joxlod: in ehe " 357736 "temporary tabl " 0 "PL/SQL MPCODE " 39392 Scrolling down a bit more in the trace file, we see the Library Cache information at the time of the error:
LIBRARY CACHE STATISTICS: (emphasis added on key areas)
namespace gets hit ratio pins hit ratio reloads invalids -------------- --------- --------- --------- --------- ---------- ---------- CRSR 4265150 0.977 496114150 0.999 155148 46115 TABL/PRCD/TYPE 40860748 0.999 80409664 0.994 190813 0 BODY/TYBD 52028 0.996 55986 0.920 3084 0 TRGR 468975 0.998 468975 0.998 76 0 INDX 54546 0.919 65318 0.867 0 0 CLST 122885 0.992 166510 0.989 0 0 OBJE 0 0.000 0 0.000 0 0 In the Library Cache Statistics information, look for 'hit ratio' percentages that would indicate fragmentation problems. The goal is to keep the 'hit ratio' as close to 100% as possible. Also review the reloads and invalids information. Lots of invalids and reloads means more flushing of memory in the library cache and can be a sign of application inefficiencies and fragmentation. On 10gR2, the V$SGASTAT view contains more detail than prior releases. An initial approach on 10gR2 is to run the SGAStat.sql query (from Note: 430473.1) every 30 minutes or so for several hours during peak activity. Comparing the results of the memory entries will help identify where allocations are growing. If explicitly setting the Heapdump event
alter system set events '4031 trace name HEAPDUMP level 536870914';
you will see a more detailed view of memory
SUBHEAP 1: desc=3800092e0
****************************************************** HEAP DUMP heap name="KSFD SGA I/O b" desc=3800092e0 extent sz=0x4258 alt=32767 het=32767 rec=9 flg=3 opc=0 . . . ****************************************************** SUBHEAP 2: desc=3a1b57a10 ****************************************************** HEAP DUMP heap name="PX subheap" desc=3a1b57a10 extent sz=0xff50 alt=32767 het=32767 rec=9 flg=2 opc=0 parent=380000030 owner=0 nex=0 xsz=0xff38 EXTENT 0 addr=3a4342860 Chunk 3a4342870 sz= 64904 free " " Chunk 3a43525f8 sz= 104 freeable "PX msg batch st" Chunk 3a4352660 sz= 104 freeable "PX msg batch st" Chunk 3a43526c8 sz= 104 freeable "PX msg batch st" Chunk 3a4352730 sz= 104 freeable "PX msg batch st" Total heap size = 65320 Bucket 1 size=104 Bucket 2 size=528 Bucket 3 size=600 Bucket 4 size=1112 Bucket 5 size=1120 Chunk 3a4342870 sz= 64904 free " " Total free space = 64904 Permanent space = 0 ****************************************************** In some cases, it is necessary to dump additional diagnostic information on a subheap. For example,
<>>
Chunk 3a0ba0480 sz= 4184 freeable "CURSOR STATS " ds=3a1a6c0d8 . . . ds 3a1a6c0d8 sz= 246856 ct= 59 39e642190 sz= 4184 39c08c728 sz= 4184 SQL> ORADEBUG SETMYPID SQL> ORADEBUG DUMP HEAPDUMP_ADDR 1 15596962008 (decimal value for 3a1a6c0d8) <>> HEAP DUMP heap name="CURSOR STATS" desc=3a1a6c0d8 extent sz=0x1040 alt=32767 het=32767 rec=9 flg=3 opc=0 parent=380000030 owner=0 nex=0 xsz=0x1040 EXTENT 0 addr=39e6421a8 Chunk 39e6421b8 sz= 4144 free " " EXTENT 1 addr=39c08c740 Chunk 39c08c750 sz= 80 freeable "kks pstat " . . . Chunk 39c08c860 sz= 336 freeable "kks cstat "
What is relevant in the Statspack/AWR report for a 4031 error?When investigating the 4031 problem using the Statspack/AWR reports, focus on the following
- time period
Database DB Id Instance Inst Num Startup Time Release RAC
~~~~~~~~ ----------- ------------ -------- --------------- ----------- --- 1460166532 cqlwh 1 28-Nov-05 20:18 10.2.0.1.0 NO Host Name: cqlstldb06.ceque Num CPUs: 4 Phys Memory (MB): 3,992 and
Load Profile
~~~~~~~~~~~~ Per Second Per Transaction --------------- --------------- Parses: 132.46 31.70 Hard parses: 0.73 0.17 Instance Efficiency Percentages (Target 100%) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Library Hit %: 99.90 Soft Parse %: 99.45 Execute to Parse %: 42.77 Latch Hit %: 99.91 Parse CPU to Parse Elapsd %: 97.19 % Non-Parse CPU: 93.91 Shared Pool Statistics Begin End ------ ------ Memory Usage %: 95.54 95.50 % SQL with executions>1: 71.11 67.96 <% Memory for SQL w/exec>1: 64.80 64.01 Further investigation of problems in the Library Cache is possible by reviewing a series of Statspack reports before and after the time of the error(s). With 10g you can find the Library Cache information in the Snapshots Report. Click the 'Performance' tab scroll to the bottom of the page. Click on the 'Snapshots' link and select a Snapshot generated by your database by clicking on the 'ID' link. Select the 'Report' tab and you will see information like the following in HTML format:
How can we determine if there are application issues causing the problem?Common problems in the application code are
Questions you should ask/consider in this case:
1. How long does it take after a restart of the database for the problem to occur?
From Note 430473.1, run script SQLStats.sql
Only a problem if you see unusually high values. Cursor with a high version_count may indicate a problem with cursor sharing. Cursors with large shareable_mem may not necessarily be a problem but recall that the shared pool allocations are limited to approximately 4k. Larger allocations put stress on the Reserved Area and make multiple requests of the heap manager (causing contention on internal latches. There are queries to investigate further in each of those possibilities.
References: Not Using Bind VariablesHere is an example of not using a bind variable in an internal test. The test code did not complete for 2 to 3 minutes
alter system flush shared_pool;
declare type rc is ref cursor; l_rc rc; l_dummy all_objects.object_name%type; l_start number default dbms_utility.get_time; begin for i in 1 .. 1000 loop open l_rc for 'select object_name from all_objects where object_id = ' || i; fetch l_rc into l_dummy; close l_rc; end loop; dbms_output.put_line ( round( (dbms_utility.get_time-l_start)/100, 2 ) || ' seconds...' ); end; / Simply changing the test code to use a bind variable improved the performance dramatically and completed in 2 or 3 seconds
alter system flush shared_pool;
declare type rc is ref cursor; l_rc rc; l_dummy all_objects.object_name%type; l_start number default dbms_utility.get_time; begin for i in 1 .. 1000 loop open l_rc for 'select object_name from all_objects where object_id = :x' using i; fetch l_rc into l_dummy; close l_rc; end loop; dbms_output.put_line ( round( (dbms_utility.get_time-l_start)/100, 2 ) || ' seconds...' ); end; / Not using bind variables will cause over usage of memory in the Shared Pool/Library Cache. You want to investigate this problem when you see issues with high hard parse counts.
<< from Statspack report>>
Load Profile ~~~~~~~~~~~~ Per Second Per Transaction --------------- --------------- Redo size: 3,092,800.46 2,563.60 Logical reads: 314,615.77 260.78 Block changes: 18,384.93 15.24 Physical reads: 7,497.42 6.21 Physical writes: 1,698.45 1.41 User calls: 4,922.77 4.08 Parses: 8,245.52 6.83 Hard parses: 141.85 0.12 Executes: 22,421.54 18.59 Transactions: 1,206.43 % Blocks changed per Read: 5.84 Recursive Call %: 92.53 Rollback per transaction %: 2.17 Rows per Sort: 5.23 From the database side, you could use CURSOR_SHARING=SIMILAR|FORCE and the literals will be replaced with bind variables behind the scenes. FORCE and SIMILAR are nearly the same, but SIMILAR will take into account the optimizer plan as well. There are other performance impacts with CURSOR_SHARING settings, so you need to test your environment before changing this parameter. CURSOR_SHARING is a dynamic parameter, but usage changes in the Shared Pool are not immediate. If you can flush the pool or restart the database, this jump start the better use of memory. Multiple child cursorsEvery child cursor allocates space in the Shared Pool. In some cases, known bugs in the past, too many child cursors were created and allocations grew beyond intended behavior.
(Version 10g)
select sa.sql_text,sa.version_count,ss.* from v$sqlarea sa,v$sql_shared_cursor ss where sa.address=ss.address and sa.version_count > 50 order by sa.version_count ; (Version 8,9) select sa.sql_text,sa.version_count ,ss.* from v$sqlarea sa,v$sql_shared_cursor ss where sa.address=ss.kglhdpar and sa.version_count > 50 order by sa.version_count ; This query will show cursors with more than 50 children and the column showing 'Y' will indicate the reason for the generation of the child cursors. The columns with a value of 'N' can be ignored in the output.
Warning: NOTE: The V$SQL_SHARED_CURSOR view includes 'Y' or 'N' columns See Note 430473.1 for a script to better interpret the information in this view. High parse ratios
It is important to identify what could produce high parsing calls: If an ORA-04031 error is associated with high parse ratios, you will also see latch contention for the library cache latch as well as indications of lots of invalidations and reloads in the Library Cache statistics in a Statspack or AWR report.
Note 1012047.6 How To Pin Objects in Your Shared Pool Community discussionFor further discussion or questions about topics in this article, please visit 'Diagnosing and Resolving ORA-4031 errors' ReferencesNOTE:115656.1 - Legacy: Wait Scenarios Regarding 'library cache pin' and 'library cache load lock'NOTE:430473.1 - ORA-4031 Common Analysis/Diagnostic Scripts [Video] NOTE:443746.1 - Automatic Memory Management (AMM) on 11g NOTE:778.1 - Troubleshooting Video Issues in MOS NOTE:76684.1 - Monitoring Open Cursors & Troubleshooting ORA-1000 Errors NOTE:1381442.1 - How to use the ORA-4031 Troubleshooting Tool on MOS NOTE:1269139.1 - SGA Re-Sizes Occurring Despite AMM/ASMM Being Disabled (MEMORY_TARGET/SGA_TARGET=0) NOTE:1355030.1 - How To Troubleshoot ORA-4031's and Shared Pool Issues With Procwatcher NOTE:208857.1 - SCRIPT - to Set the 'SESSION_CACHED_CURSORS' and 'OPEN_CURSORS' Parameters Based on Usage NOTE:208918.1 - SCRIPT - to Gauge the Impact of the SESSION_CACHED_CURSORS Parameter BUG:4994956 - NUMBER OF CONFIGURED SHARED POOL SUBPOOLS IS NOT CORRECT BUG:3352753 - ORA-600 [KCBLIBR_1] FOLLOWED IMMEDIATELY BY ORA-4031 NOTE:274496.1 - ora-7445 and ora-4031 in 9.2.0.5 and 10g if SESSION_CACHED_CURSORS is used BUG:3663344 - V$SGASTAT (X$KSMSS) SHOWS ENORMOUS VALUE FOR 'TRANSACTION CO' MEMORY AREA NOTE:947152.1 - How To Determine Granule Size NOTE:403616.1 - Many Child Cursors create in 10.2.0.3 on Windows and Linux NOTE:1012047.6 - How To Pin Objects in Your Shared Pool NOTE:377143.1 - How to check what automatic statistics collection is scheduled on 10g NOTE:287059.1 - Library Cache Pin/Lock Pile Up in Pre-10g versions NOTE:34579.1 - WAITEVENT: "library cache pin" Reference Note NOTE:146599.1 - Diagnosing and Resolving Error ORA-04031 on the Shared Pool or Other Memory Pools [Video] NOTE:459694.1 - Procwatcher: Script to Monitor and Examine Oracle DB and Clusterware Processes NOTE:69925.1 - PINNING ORACLE APPLICATIONS OBJECTS INTO THE SHARED POOL NOTE:232443.1 - How to Identify Resource Intensive SQL ("TOP SQL") NOTE:270097.1 - ORA-4031 and Very Large Library Cache in Oracle 9.2 with Session_cached_cursors set. NOTE:270935.1 - Shared pool sizing NOTE:4031.1 - OERR: ORA-4031 "unable to allocate %s bytes of shared memory ("%s","%s","%s")" |