Since Oracle 9.2 the shared pool can be “partitioned” into multiple parts. This was probably done for relieving shared pool latch contention for crappy applications (which use shared pool latches too much due bad cursor or connection management).

The “partitions” are called shared pool subpools and there can be up to 7 subpools. Each subpool is protected by a separate shared pool latch and each subpool has its own freelists and LRU list. If you are interested in more details, a good starting point is this whitepaper by Oracle.

There are few different ways for detecting how many subpools you have in use. The more convenient ones are here:

You could query X$KGHLU which has a line for each shared pool subpool and (from 10g) also java pool if it’s defined:

The “kghlushrpool” column, which is 1 for shared pool subheaps and 0 for java pool, isn’t there in 9i (and in 9i the java pool apparently is not reported in x$kghlu anyway).
The reason why I don’t just count all matching lines from x$kghlu but use count distinct instead is that in Oracle 10.2.0.1 there are 4x more lines reported in this x$table. There’s an additional concept called sub-sub-pool starting from 10.2 where each shared pool sub-pool is split futher into 4 areas (allocations with different expected lifetime/durations go into different sub-sub-pools, but the same sub-pool latch protects all activity in sub-sub pools too). But in 10.2.0.1 the x$kghlu reports all sub-sub-pools too for some reason. The whitepaper from Oracle mentioned above explains this in more detail.

So from above output I see that in my instance all 7 shared pool subpools are in use. Oracle determines the number of needed subpools (during instance startup) based on your shared pool size and cpu_count. IIRC in 9.2 if you had 4 CPUs or more AND the shared_pool_size was bigger than 256 MB then 2 subpools were used, in 10g shared_pool_size had to be bigger for that, 512 MB I think and in 11g its 1GB. I don’t recall the exact threshold values and that’s not really important as you can see yourself how many subpools are in use with the above query.

For sake of this experiment I set the _kghdsidx_count variable to 7, this parameter can be used to force the number of subpools you want. In 9.2 days it was actually quite common to set this back to 1 IF you had ORA-4031 errors AND the reason was diagnosed to be free space imbalance between subpools. However since 10g this has been almost unnecessary as Oracle has improved their heap management algorithms.

The script above queries few X$ tables to show the value of this hidden parameter.

So far the two above approaches have required access to X$ tables which usually means you need to be logged on as SYSDBA. What if you don’t have such access?

In such case you can work this out pretty reliably by looking into how many of the shared pool latches are actually in use. All 7 latches are always there, even if you have less subpools in use, that number is hardcoded into Oracle. But you can see how many latches have a significant number of gets against them.

In my case its evident that all latches are in use, they all have significant number of gets against them:

It’s ok to see some latch gets against the latches of unused subheaps, but this number should be much much smaller than others. The reason appears to be that all subheap latches are taken when shared pool is allocated and when shared pool resize operations are done.

For example, this is what I see after setting the number of shared pool subpools to 2 in my test database (and running some hard parsing workload):

Note that this article doesn’t aim to explain all the basics of ORA-4031 troubleshooting, I’ll talk about the subpool utilization imbalance problem only. If you haven’t read metalink note 396940.1 – “Troubleshooting and Diagnosing ORA-4031 Error” yet, I recommend to do this first and then read my comments here.

“n” shows how many bytes we tried to allocate when ended up with the failure. Italic strings can show various different values but essentially they’re just some metadata describing for what did we try to allocate that memory.

Note the two bold pieces. The “shared pool” means that we tried to make the allocation from shared pool (if you have problems with other pools you can see there “large pool”, “streams pool”, “java pool” as well).
The “2” in “(2,0)” means that the failure happened in shared pool sub pool number 2 and the “0” shows sub-sub-pool number 0.

Sometimes the error happens just due heavily undersized shared pool (combined bad cursor management or some incorrect parameter values). In such cases you would see the shared pool free memory drop to near-zero in V$SGASTAT.

However, sometimes you can have ORA-4031’s even when you see plenty of free space available in V$SGASTAT. What’s the issue with that?

This case happens mainly for two reasons:

1) Shared pool free memory fragmentationThere is no big enough free chunk available even after flushing out unpinned chunks from LRU list. In other words, you have a lot of small free chunks scattered around in different places in shared pool but there is no single big enough chunk available for acommodating our allocation. I will talk about troubleshooting this problem in a separate post.

2) Unbalanced memory usage / free memory in different shared pool subpoolsThis is what I’m explaining in current post.

Hmm… 1 and 2 look ok as I have subpool 1 and 2 defined in the instance, but why is there a subpool 0 also reported? (from 10g anyway).

This is due a little feature in Oracle. When you start the instance in 10g, then not all memory reserved for shared pool is immediately given to subpool heaps. Some memory is reserved for individual subpool growth. This allows some subpools to grab more memory than others if they have more allocations after instance startup. This may be useful in cases where due some specific issue some subpool always needs much more memory than others. On the other hand, I have not seen a subpool heap give memory back to some other subpool so if one subpool allocates all of the reserved memory after instance start due some application startup activity, then the other pools may remain too small for the whole lifetime of the instance.

So, if you have ORA-4031 out of shared pool memory errors or suspect that shared pool memory pressure is the cause of some performance problem (like shared pool latch contention and excessive library cache evictions/reloads) then you’d want to monitor shared pool memory breakdown at the subheap level.

And (finally) I can introduce a little script sgastatx.sql which queries X$KSMSS and formats the output for better readability.

The script takes one parameter, what memory allocation reasons to report (% would report all):

I will start with “total” which just reports me the shared pool totals and doesn’t break down by allocation reason.

Of course sometimes you’d want to know how the memory usage breakdown changes over time, for that you’d need to write a little collector script which dumps the data into some table and visualize it later on, like I have done for regular V$SGASTAT data with my PerfSheet tool ;-)

Thank you for excellent script and explanation of sub-pools. I have one question:
>On the other hand, I have not seen a subpool heap give memory back to some other subpool so if one subpool allocates all of the reserved memory after instance start due some application startup activity, then the other pools may remain too small for the whole lifetime of the instance.
What happens in case of ASMM/AMM?

If you remember I was trying to resolve the shared pool fragmentation problem by reducing the number of subpools from 4 to 2.But then I found a few weired thing in shared pool i.e. db block buffers headers consuming lot of space so I replaced it with db_cache_size and this component got removed from the shared pool and I also reduced the shared pool min size alloc to solve this issue.

Santosh,
If you don’t mind, could you please share your research to investigate the issue and how you concluded that this issue is related to db block buffers headers’ space consumption in shared pool?

Both v$sgastat (or my sgastatx script which uses x$ksmss) and also shared pool heapdumps/x$ksmsp should show “db_block_buffers” – a component *held in shared pool* if old fashioned memory management is used (9i only I think).

The issues was avg -10 4031 per day and all the errors were only in subpool 1 only.We configured 4 subpools in shared pool.

while checking the memory consumption in shared pool , came across this fixed component “db block buffers headers” consuming significant amount of memory almost 20%, in shared pool.This component got initiated because of db_block_buffers setting, so after few tests , observed that if we replace db_block_buffers with db_cache_Size , we can get rid of this component , allowing that memory for user programs.

Hi Tanel,
In RAC environment, 2 node, but application only connect to node1 and node2 is idle.
if I check the free memory of shared pool (use sgastatx “free memory”), it show that node1 have much more free memory than node2, even the node 2 is idle.
Please advise?

Thanks for this great post. I have been fighting with shared pool for last few days but shared pool doesn’t want to release any free memory it has. This is a 11g single instance database with ASM. I started up this database with Automatic SGA memory management with 1.5GB of total SGA. Out of 1.5g, shared pool took about 850M of memory leaving only 400M some memory for buffer cache. I am trying to adjust this memory without restarting database but for some reason Oracle thinks it really needs 800+ memory. When I run query on v$sgastat, I can see that there is about 600+ MB of memory free in shared pool. I have flushed the shared pool, disabled ASMM(Automatic SGA), re-enabled ASMM but shared pool still will not release this free memory to buffer cache. Does anyone has any idea how to force shared pool to release this memory without restarting database? Any help would be appreciated. Thanks.

Is there any way to know what is the current used and free memory out of the shared_pool_size when I get the ORA-04031 error?

That is, for example, if I get ORA-04031: unable to allocate 4328 bytes of shared memory (“shared, what is the current free memory in the shared pool? Obviously it ls less than 4328 bytes then but over time, some memory got freed up ‘coz I was able to re-run the same job that failed with this error.

Is this information available in one of the DBA_HIST views?

FYI, I have SGA_TARGET = 0 so I am not using automatic memory management.

Do we have any control over on which subpool the parse information is stored. Recently i was working on a 4031 on subpool 1 for a session and flushed shared pool and only to find out the subpool 1 is quickly filling up(even though other subpools were having plenty of space) and user session is getting the same error(even after logout/login, user connection is on subpool 1 only ). I heard that hash algorithm is used to redirect to particular subpool based on session information . Is that true? Can DBA control anything abt it?

No unfortunately there’s no control – other than reducing the number of subpools with (_kghdsidx_count) to avoid bugs/issues. Yep there ought to be some hash value used (and some KGL object directory) which determines where the allocations are made from – but it very likely depends on the type of allocation itself (there are still some allocations which can’t go to any subpool and can’t be “striped” across subpools). You could try to open another session (while keeping the old one logged in too) to get a new SID and Oracle PID values and see if this results in a different subpool usage. Note that the whole subpool thing has had plenty of changes since it was introduced in Oracle 9i.

If you want to avoid such errors – patch to latest DB levels or just reduce the number of subpools. Of course after reducing the shared pool usage if possible (less hard parses etc etc etc). I’ve troubleshooted issues where going from 4 to 2 subpools avoided the issues (and going back to 1 would be the “best” unless you need multiple subpools due to heavy shared pool latch usage or NUMA reasons). If you don’t see almost any shared pool latch contention, you likely do not need multiple subpools.

Do u have a x$ view or query to find out the unpinned/free-able memory in various subpools/shared pool, which is safe to run on a busy DB?. We dont have _library_cache_advice set, so x$kglmem does not have any data. Heap dump is also ruled out as ours is a VVLarge&Busy db instance and we do not want to take a heapdump.

Also, above it is noted that “distinct kghluidx” is used because there would be >1 row per subpool,referring to each duration (which means x$kglhu,with 7 subpools will have 28 lines or so and not seven)… but J lewis’s ‘oracle core’ book (which you reviewed) ,on Page 190 says even when durations are enabled, x$kglhu will have only one line per subpool as there is a single LRU list per subpool, irresepective of # of durations).

In Oracle 10.2.0.1 for some reason up to 28 rows were reported in X$KGHLU. Afterwards (in 10.2.0.2+ I think) only 7 were reported. So it was a reporting issue / bug with X$KGHLU. The heapdump would tell you the truth. The durations are controlled by _enable_shared_pool_durations parameter.

There’s no low-impact way to know the details. X$KSMSP has been improved lately (11.2.0.2+ I think) to use a kghdmp_new() routine that supposedly holds the shared pool latches for less time at a time, but I wouldn’t still dare to use it in production in most cases. I’d rather gather and plot X$KGHLU and X$KSMLRU views (and the “sql area evicted” / “CCursor + sql area evicted”) metrics and try to use these as an early warning system.