Monday, January 28, 2013

What is fixed size and variable size in the show sga output

When we SELECT * from V$SGA (or show sga), we get the values of Fixed Size, Variable Size, Database Buffers and Redo Buffers. For e.g,

SQL>SELECT * FROM v$sga;
NAME                               VALUE
----------------------------- ----------
Fixed Size                       2070096
Variable Size                  234883504
Database Buffers               687865856
Redo Buffers                     6316032

While the "Database Buffers" is a product of db_cache_size and db_block_size or  the "Redo Buffer" is the value of log_buffer in INIT.ora, What is this fixed and variable size? How do wecompute Fixed Size and the Variable Size of a SGA? What are they indicative of? Here is theanswer for that. 
Fixed SGA
The fixed SGA is a component of the SGA that varies in size from platform to platform and
release to release.  It is “compiled” into the database.  The fixed SGA contains a set of
variables that point to the other components of the SGA and variables that contain the
values of various parameters like db_block_size and so on (constants basically for the currently mounted database).The size of the fixed SGA  have  no control and it is generally very small.  Think of this area as a “bootstrap” section of the SGA, something Oracle uses internally to find the other bits and pieces of the SGA.
The size of the fixed portion is constant for a release and a platform of Oracle, that is, it cannot be changed through any means such as altering the initialization parameters The fixed size component is out of your control. It varies from release to release and platform to platform. Thus It contains a collection of objects that point to other areas inside the rest of the SGA -- think of it as the "road map" to the rest of the SGA.

Variable portion
The variable portion is called variable because its size (measured in bytes) can be changed.For consideration of the variable size, it is useful to look at v$sgastat 

SQL>compute sum of bytes on pool
SQL>break on pool skip 1
SQL>set pages 200
SQL>set pause on
SQL>select pool, name, bytes  from v$sgastat  order by pool, name;
POOL         NAME                               BYTES
------------ ----------------------------- ----------
java pool    free memory                      4194304
************                               ----------
sum                                           4194304

large pool   PX msg pool                      1076000
             free memory                      3118304
************                               ----------
sum                                           4194304

shared pool  1:kngisga                          32088
             AQ Propagation Scheduling          16000
             ASH buffers                     16252928
             ASM file                           19200
             ASM map operations                  3616
             ASM rollback operations             2592
             AW SGA                                40
             AWR Table Info (KEW layer            872
             Active Session History -            1528
             ArchLog Dest Array                  7656
             Array of cached attr                 400
             Auto tune undo info                   96
             BRANCH TABLE SEGMENTED AR         194016
             Background process state              48
             Banner Storage                      2048
             CCursor                         13817392
             CGS system incarn array            24864
             Checkpoint queue                  821248
             Cleanup state objects                384
             Client ID trace setting h            536
             Client ID trace settings            3872
             Closed Thread SCN Bitvec            8448
             Cursor Stats                     1152280
             DBWR event stats array               192
             DISPATCHER CONFIG ENTRY               88
             DISPATCHER CONFIG TABLE             2048
             DISPATCHERS INFO                    2400
             DML lock                          195872
             ENQUEUE STATS                      11928
             File Space Usage                   11336
             FileIdentificatonBlock            449664
             FileOpenBlock                    1600704
             Global Context Heap descr            400
             HTTP fixed headers                    72
             Heap0: KGL                       4569328
             JSX SGA                              112
             KCB Table Scan                        40
             KCB Table Scan Bitmap                248
             KCB Table Scan Buffer            3981120
             KCB buffer wait statistic           3352
             KCB incremental ckpt entr            512
             KCB private handles                 1800
             KCB undo bitvec                     4096
             KCB where statistics arra          25888
             KCK type array                       936
             KESTB existence bitvec             16384
             KESTB existence bitvec se            128
             
                     ..........................

             session idle latches                1280
             sessions                          932968
             set_descriptor_array               43776
             sga dev dict                          56
             sga listelement                     2048
             sga node map                          16
             sim cache nbufs                      640
             sim cache sizes                      640
             sim kghx free lists                    8
             sim lru segments                    5120
             sim segment hits                    5120
             sim segment num bufs                2560
             sim state object                      40
             sim trace buf                       5144
             sim trace buf context                200
             sim_knlasg                          1280
             simulator hash buckets            263168
             simulator hash latch               20480
             simulator latch/bucket st          11264
             slave class sga anchor                88
             sort segment handle                 3872
             spfile callback table: ks            448
             spfile cleanup structure           16760
             sql area                        57846312
             sql area:KOKA                      44528
             sql area:PLSQL                   1687768
             stat hash values                    1456
             state objects                       5680
             sys event stats                   198688
             sys event stats for Other         198688
             system default language h            568
             table definiti                     27264
             temp lob duration state o           3296
             temporary foreign ref               2848
             temporary table lock                3744
             threshold ale                      12192
             time manager context                  40
             trace buf hdr xtend                35088
             trace buffer                      655360
             trace buffer header array          35088
             trace events array                 68000
             trace_knlasg                         504
             transaction                       417296
             trigger condition node                72
             trigger defini                     33936
             trigger inform                      4224
             trigger source                     30624
             txncallback                        54208
             type object de                    789536
             where to latch num map              7304
             work area tab                     270144
             writes stopped lock conte             24
             writes stopped lock state             24
             x$ksmfs table                         24
             x$rule_set                         17608
             xscalc                              4000
             xslongops                           2592
             xsoqmehift                          2848
             xsoqojhift                          4128
             xsoqophift                          2848
             xsoqsehift                          3104
             xssinfo                             5952
************                               ----------
sum                                         226497208

             buffer_cache                   687865856
             fixed_sga                        2070096
             log_buffer                       6316032
************                               ----------
sum                                         696251984
621 rows selected.
SQL>
So, in general -- the fixed size is computed for you when Oracle is compiled.  You can do nothing to affect it.  The variable size is MOST affected by java_pool_size + large_pool_size + shared_pool_size but other parameters will contribute to it (eg: every control_file will consume 256 bytes of variable size memory.  If you have 4 control files, the SGA will have 1024 bytes set aside for them) in a small way.

Different recover scenario during incomplete database recovery

If you need to recover your database to a point in time by scn, sequence or time, you can use the following query to see the relation between time-scn-sequence, after restoring your database from a proper backup.

SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YY HH24:MI:SS';


SQL> select NAME, SEQUENCE#, FIRST_TIME, FIRST_CHANGE#, NEXT_TIME, NEXT_CHANGE# from v$archived_log where SEQUENCE# > 100;

NAME                                              SEQUENCE# FIRST_TIME        FIRST_CHANGE# NEXT_TIME         NEXT_CHANGE#
------------------------------------------------ ---------- ----------------- ------------- ----------------- ------------
D:\DATA\ARCHIVE\ARC00101_0800466637.001                 101 20-01-13 20:30:06       3982521 21-01-13 04:30:27      4011361
D:\DATA\ARCHIVE\ARC00102_0800466637.001                 102 21-01-13 04:30:27       4011361 21-01-13 19:47:21      4057937
D:\DATA\ARCHIVE\ARC00103_0800466637.001                 103 21-01-13 19:47:21       4057937 22-01-13 00:45:42      4080184
D:\DATA\ARCHIVE\ARC00104_0800466637.001                 104 22-01-13 00:45:42       4080184 22-01-13 09:00:30      4108498
D:\DATA\ARCHIVE\ARC00105_0800466637.001                 105 22-01-13 09:00:30       4108498 22-01-13 22:00:05      4150127
D:\DATA\ARCHIVE\ARC00106_0800466637.001                 106 22-01-13 22:00:05       4150127 23-01-13 03:40:58      4172819
D:\DATA\ARCHIVE\ARC00107_0800466637.001                 107 23-01-13 03:40:58       4172819 23-01-13 15:18:23      4211524
D:\DATA\ARCHIVE\ARC00108_0800466637.001                 108 23-01-13 15:18:23       4211524 23-01-13 15:22:48      4211829

8 rows selected.


You can modify the where clause depending on your needs. 

SEQUENCE# gives the sequence number of the archive log. FIRST_CHANGE# and NEXT_CHANGE# specify the first and last System Change Number (SCN); FIRST_TIME and NEXT_TIME specify the starting and ending time of that archivelog. Regarding to these information you can decide any of the following recover operations:

RMAN> recover database until sequence 107;
RMAN> recover database until SCN 4172819;
RMAN> recover database until time '23-01-13 03:40:58'

Or if you want to manually control recover process with specifying archive logs one by one, you can use "until cancel" clause in SQL. This recovery process continues until you cancel. If your archive logs are not on their default path you can specify the full path of the archive logs in this recovery process.

SQL > recover database until cancel;

Thursday, January 24, 2013

How to start/stop Clusterware service in Oracle 10gr2 RAC

Oracle Clusterware services in a Windows RAC environment should be allowed to start automatically, ideally, with the help of the Oracle Process Manager Service (OPMD). If it is not installed already then you can do the following on the concerned node's command prompt - opmd.exe -install .

Oracle Process Manager Daemon (OPMD)—OPMD is registered with the Windows Service Control Manager (WSCM) and the startup of all Oracle Clusterware services is dependent on OPMD. On system startup, and after the default time period of 60 seconds has elapsed, OPMD automatically starts all of the registered Oracle Clusterware services. This startup delay enables other services to start that are outside of the scope of Oracle control, such as storage access, anti-virus, or firewall services. You can set OPMD to start manually. However, this will delay the startup of the rest of the affected Oracle Clusterware components.The opmd.exe allows the creation of a new service called the ‘Oracle Process Manager’. The Oracle Process Manager will control the startup of the clustering services to prevent the 'OracleCSService' from starting up before the rest of the OS services have started.

You can check the oracle background process in windows by,
set line 100
col program format a20
col description format a32
select A.SID, A.STATUS, A.PROGRAM, B.NAME, B.Description
from v$session A, v$bgprocess B
where A.PADDR=B.PADDR
and A.type='BACKGROUND';


Following is an outline of how to start Oracle Clusterware services manually if it should become necessary to do so.


1- Try starting crs with "crsctl start crs" command from $CRS_HOME\bin directory 
OR
2.  Start the following services from Windows 'Services' or by using the Windows command 'net start at the command prompt:
            a.  If OPMD is present, then start the service:  OracleProcessManagerService.  In addititon to OPMD, start the  OracleClusterVolumeService
if you are using Oracle Cluster File System (OCFS) in your environment.

             b. If OPMD is not present then start the following services in the order provided:
                    OracleObjectService
                    OracleClusterVolumeService (if using OCFS)
                    OracleCSService
                    OracleEVMService
                    OracleCRService


To stop these services, the same instructions can be followed in reverse.

The reverse command for 'crsctl start crs' is 'crsctl stop crs.'  

However, it is advised that when stopping CRS the resources managed by the OCR are safely brought down before stopping the underlying clusterware services.  


Note that issuing 'crsctl stop crs' without first issuing 'srvctl stop nodeapps -n ' command will cause the ons.exe to continue running and so the ons.exe must be stopped from task manager before clusterware services are considered completely down. 

Wednesday, January 23, 2013

An example for Incomplete recovery through rman

An incomplete recovery is a type of recovery in which only a part of redo log , archive log data are applied to a restored backup in order to bring it to a specified time, sequence number or SCN prior to the failure. You will lose all works done after that specified time.You must run the database in ARCHIVELOG mode in order to perform an incomplete recovery.

The incomplete recovery consists of four steps:
1. Mount the database.
2. Restore all the datafiles.
3. Recover the database until the desired point.
4. Open the database with a resetlogs.
You can use the incomplete recovery with:
1- User managed backups: in this case you have three options: UNTIL TIME Recovery (before a specific time), UNTIL CANCEL recovery (before the one specified corrupted, missed archive log or redo log file), UNTIL CHANGE Recovery (before the one specified SCN or system change number).
2- RMAN backups: in this case you have three options: UNTIL TIME Recovery (before a specific time), UNTIL SEQUENCE recovery (before the one specified corrupted, missed archive log or redo log file), UNTIL SCN Recovery (before the one specified SCN or system change number).
Practical example:-
First be sure that the database is in ARCHIVELOG mode. Then we will create a table called test_incomplete_rec to test the incomplete recovery.
C:\Users\Admin>set ORACLE_SID=craze
C:\Users\Admin>sqlplus sys as sysdba
SQL*Plus: Release 10.2.0.4.0 - Production on Wed Jan 23 15:13:40 2013
Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.
Enter password:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select archiver from v$instance;
ARCHIVE
-------
STARTED
SQL> set time on
15:13:55 SQL> create table scott.test_incomplete_Reco as select * from dba_Tables;
Table created.
15:14:03 SQL>
Now take a full rman database backup ,
C:\Users\Admin>set ORACLE_SID=craze
C:\Users\Admin>rman target sys/sys
Recovery Manager: Release 10.2.0.4.0 - Production on Wed Jan 23 15:14:26 2013
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
connected to target database: CRAZE (DBID=781012619)
RMAN> show all;
using target database control file instead of recovery catalog
RMAN configuration parameters are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO 'D:\ORACLE\DATABASE\SNCFCRAZE.ORA'; # default
Note:- Here rman having the default configuration , i changed nothing and the backup set will goes to $ORACLE_HOME/database folder.
RMAN>  backup database;
Starting backup at 23-JAN-13
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=134 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=D:\DATA\CRAZE\SYSTEM01.DBF
input datafile fno=00003 name=D:\DATA\CRAZE\SYSAUX01.DBF
input datafile fno=00002 name=D:\DATA\CRAZE\UNDOTBS01.DBF
input datafile fno=00005 name=D:\DATA\CRAZE\EXAMPLE01.DBF
input datafile fno=00004 name=D:\DATA\CRAZE\USERS01.DBF
channel ORA_DISK_1: starting piece 1 at 23-JAN-13
channel ORA_DISK_1: finished piece 1 at 23-JAN-13
piece handle=D:\ORACLE\DATABASE\01O055C8_1_1 tag=TAG20130123T151520 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:26
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 23-JAN-13
channel ORA_DISK_1: finished piece 1 at 23-JAN-13
piece handle=D:\ORACLE\DATABASE\02O055D2_1_1 tag=TAG20130123T151520 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 23-JAN-13
RMAN>

Now drop the table , 
15:17:01 SQL> drop table scott.test_incomplete_Reco;
Table dropped.
Note:- The table got dropped at 15:17:01 
15:17:15 SQL> select * from scott.test_incomplete_Reco;
select * from scott.test_incomplete_Reco
 *ERROR at line 1:
ORA-00942: table or view does not exist
15:17:22 SQL>

Now perform an incomplete recovery through rman ,
C:\Users\Admin>rman target sys/sys
Recovery Manager: Release 10.2.0.4.0 - Production on Wed Jan 23 15:21:13 2013
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
connected to target database: CRAZE (DBID=781012619)
RMAN> run
2> {
3> shutdown immediate;
4> startup mount;
5> sql "alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss'' ";
6> set until time '2013-01-23 15:16:00';
7> restore database;
8> recover database;
9> alter database open resetlogs;
10> }
using target database control file instead of recovery catalog
database closed
database dismounted
Oracle instance shut down
connected to target database (not started)
Oracle instance started
database mounted
Total System Global Area     931135488 bytes
Fixed Size                     2070096 bytes
Variable Size                234883504 bytes
Database Buffers             687865856 bytes
Redo Buffers                   6316032 bytes
sql statement: alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''
executing command: SET until clause
Starting restore at 23-JAN-13
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to D:\DATA\CRAZE\SYSTEM01.DBF
restoring datafile 00002 to D:\DATA\CRAZE\UNDOTBS01.DBF
restoring datafile 00003 to D:\DATA\CRAZE\SYSAUX01.DBF
restoring datafile 00004 to D:\DATA\CRAZE\USERS01.DBF
restoring datafile 00005 to D:\DATA\CRAZE\EXAMPLE01.DBF
channel ORA_DISK_1: reading from backup piece D:\ORACLE\DATABASE\01O055C8_1_1
channel ORA_DISK_1: restored backup piece 1
piece handle=D:\ORACLE\DATABASE\01O055C8_1_1 tag=TAG20130123T151520
channel ORA_DISK_1: restore complete, elapsed time: 00:00:26
Finished restore at 23-JAN-13
Starting recover at 23-JAN-13
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished recover at 23-JAN-13
database opened
RMAN>
Database has been opened in resetlogs and we can check the existence of the table, 

15:24:58 SQL> select count(1) from scott.test_incomplete_Reco;
 COUNT(1)
----------
      1594
15:25:31 SQL>


Monday, January 21, 2013

what is granules in sga ?

With the dynamic SGA infrastructure, the size of the buffer cache, the shared pool, the large pool, and the process-private memory can be changed without shutting down the instance.With dynamic SGA, the unit of allocation is called a granule
Granules are introduced in oracle 9.0.1 . Components, such as the buffer cache, the shared pool, the java pool, and the large pool, allocate and deallocate space in units of granules . Oracle tracks SGA memory use in integral numbers of granules, by SGA component. For example,

If you specify a size for a component that is not a multiple of granule size, then Oracle rounds the specified size up to the nearest multiple. For example, if the granule size is 4 MB and you specify DB_CACHE_SIZE as 10 MB, you will actually be allocated 12 MB.


Granule size is determined by total SGA size. On most platforms, the size of a granule is 4 MB if the total SGA size is less than 128 MB, and it is 16 MB for larger SGAs. There may be some platform dependency, for example, on 32-bit Windows NT, the granule size is 8 MB for SGAs larger than 128 MB.



The granule size that is currently being used for SGA can be viewed in the view V$SGA_DYNAMIC_COMPONENTS. The same granule size is used for all dynamic components in the SGA. We can also retrieve information from v$sgainfo from oracle 10.1 onwards

SQL>select COMPONENT,GRANULE_SIZE from V$SGA_DYNAMIC_COMPONENTS;

COMPONENT                      GRANULE_SIZE
------------------------------ ------------
shared pool                        16777216
large pool                         16777216
java pool                          16777216
streams pool                       16777216
DEFAULT buffer cache               16777216
KEEP buffer cache                  16777216
RECYCLE buffer cache               16777216
DEFAULT 2K buffer cache            16777216
DEFAULT 4K buffer cache            16777216
DEFAULT 8K buffer cache            16777216
DEFAULT 16K buffer cache           16777216
DEFAULT 32K buffer cache           16777216
ASM Buffer Cache                   16777216

13 rows selected .

Here granule_size is taken as 16MB.

Adding Granules to Components
A database administrator grows a component's SGA use with ALTER SYSTEM statements to modify the initialization parameter values. Oracle takes the new size, rounds it up to the nearest multiple of 16MB, and adds or takes away granules to meet the target size. Oracle must have enough free granules to satisfy the request. If the current amount of SGA memory is less than SGA_MAX_SIZE, then Oracle can allocate more granules until the SGA size reaches SGA_MAX_SIZE.

The minimum SGA size is three granules, based on:

  1. One Granule for Fixed SGA (includes redo buffers)
  2. One Granule for Buffer Cache
  3. One Granule for the Shared Pool

Following pools can be dynamically resized , 

Stream pools can not dynamically resized  in 10.1 

Saturday, January 19, 2013


Automatic Shared Memory Management in Oracle 10g

Oracle instance in memory consists of two major areas SGA (system/shared global area)and PGA (program/private global area). The SGA is shared by all sessions and consists of a few pools for different purposes. A few of them are as follows:

1. DEFAULT buffer cache :- used to store oracle data blocks when they are read or updated.
2. DEFAULT nK buffer cache :- used to store oracle data blocks having different size then the default block size (db_block_size) - (non -ASMM)
3. KEEP buffer cache :- used to store oracle data blocks from the objects which are not supposed to age out from memory. (non -ASMM)
4. RECYCLE buffer cache :- used to store oracle data blocks from the objects which are not supposed to be kept in the memory. (non -ASMM)
5. log buffer cache :- used to store redo entries to reconstruct the operations in case of an instance crash. (non -ASMM)
6. shared pool :- used to parse and store session queries, define execution plans for queries etc.
7. large pool :- used for backup/recovery operations and batch job processing etc.
8. java pool :- All session's java related activities are done here.
9. streams pool :- used for oracle streams.

Sizing these pools manually in the SGA is a great pain and it is almost impossible to use all available memory efficiently to different pools. Lets take a scenario where the database is being used for OLTP application in the daytime and there are some huge batch jobs scheduled to run every night. We have 1G of memory available for SGA out of which we have given 400m to the DB Buffer Cache, 300m to Shared Pool, 100m to Large Pool and rest of the memory i.e. 200m to other pools in the SGA.

In the daytime the DB Buffer Cache is being used extensively for OLTP transactions and a very little of Large Pool say 5 to 10 megabytes. Keeping this in view, even when DB Buffer Cache is in contention and 400m is not sufficient enough for it we are wasting a lot of memory in Large Pool where nothing is being happening.

While during nights when there is no OLTP activity and we need more memory for Large Pool, a lot of memory is being wasted in the DB Buffer cache.
Having this problem in hand now lets go through the ASMM (Automatic Shared Memory Management) feature introduced in Oracle 10g and see how it can help us with our problem.

ASMM when switched on, it controls the sizes of the certain components in the SGA by making sure they get the memory they need and it does that by shrinking the components which are not using all of memory allocated to them and growing the ones which need more then the allocated memory. ASMM adopts to the workload changes and maximize the utilization of the memory. This happens with the help of MMAN (Memory Manager) background process which is all the time capturing the workload during the instance run and uses the memory advisers to decide what should be size of components.

Components like db_nk_caches, keep/recycle buffer cache and log buffer cache are manually tuned. ASMM does the auto tuning for the following pools:

1. DEFAULT buffer cache
2. Shared Pool
3. Large Pool
4. Java Pool
5. Streams Pool (10g R2+)

When ASMM is disabled the following initialization parameters are used to set the sizes for auto tuned pools in SGA. In oracle 10g these initialization parameters are called "auto tuned parameters".

1. db_cache_size
2. shared_pool_size
3. large_pool_size
4. java_pool_size
5. streams_pool_size

To switch to ASMM you need to set the initialization parameters SGA_TARGET to a non-zero value which must be less than or equal to value of parameter SGA_MAX_SIZE.

$ sqlplus / as sysdba
SQL> alter system set sga_max_size=1G scope=spfile;
System altered.
SQL> alter system set sga_target=500m scope=both;
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
 Total System Global Area 1073741824 bytes
Fixed Size                  1223540 bytes
Variable Size             738198668 bytes
Database Buffers          327155712 bytes
Redo Buffers                7163904 bytes
Database mounted.
Database opened.
SQL> show parameter sga_target
 NAME                        TYPE        VALUE
--------------------------- ----------- ---------------------
sga_target                  big integer 500M
SQL> show parameter sga_max_size
 NAME                        TYPE        VALUE
--------------------------- ----------- ---------------------
sga_max_size                big integer 1G
SQL>

According to the configuration we just made the SGA_TARGET is 500m, which means that the total size of SGA will be 500m and after allocating the defined sizes to the non-auto tuned pools and memory areas Oracle will dynamically manage all the auto tuned pools in the remaining memory space. But the total size of the SGA will never exceed 500m. I have met people having a perception that sga_target is the limit for the SGA but if the current_sizes are not enough for the components in the SGA it may grow upto sga_max_size which is incorrect. The SGA will stay in the boundaries of sga_target, period.
The reason why sga_max_size is usually larger then sga_target is the relationship between these two and the static nature of the sga_max_size parameter. Lets say you have set both sga_target and sga_max_size to 500m initially but later on after a couple of months you find out that 500m is not enough memory for your components to be managed in an efficient manner. Now if you want to increase the sga_taget to 1G, you will have to increase the sga_max_size to 1G also because sga_target cannot be larger then sga_max_size. But othe other hand if initially you set sga_target to 500m and sga_max_size to 1G then you have a window of at lease 500m to increase your sga_target without shutting down your database. sga_max_size is nothing more then a maximum limit which defines how big your sga_target can be, it doesn't effect memory allocation for the SGA in the oracle instance. Whenever an oracle instance is started it allocates the SGA memory equal to the value of sga_target, so it doesn't really matter how big you set your sga_max_size.

Now lets come back to our example where we have set sga_target to 500m, it doesn't mean all of this 500m will be used for auto tuned pools. The memory that will be used for the auto tuned pools is (sga_target - sum of non-auto tuned areas sizes). ASMM is not suppose to touch the size of manually tuned memory areas. If the total size of all non-auto tuned areas (log buffer cache, keep/recycle buffer cache etc) is 100m, then rest of 400m will be used for adjusting the sizes of auto tuned pools according to the workload.

After we enable the automatic memory management Oracle start managing the pools for us and set reasonable sizes for all the pools according to their nature and the type of work they do. Lets have a look at the current allocation of the auto tuned pools:

$ sqlplus / as sysdba
 SQL> show parameter db_cache_size
 NAME                        TYPE        VALUE
--------------------------- ----------- ---------------------
db_cache_size               big integer 0
SQL> show parameter pool_size
 NAME                        TYPE        VALUE
--------------------------- ----------- ---------------------
global_context_pool_size    string
java_pool_size              big integer 0
large_pool_size             big integer 0
olap_page_pool_size         big integer 0
shared_pool_size            big integer 0
streams_pool_size           big integer 0
SQL>     
           
SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
 COMPONENT                          SIZE_MB
------------------------------- ----------
shared pool                            172
large pool                               4
java pool                                4
streams pool                             0
DEFAULT buffer cache                   292

Notice all the auto tuned parameters are set to 0 (we will discuss about this later). v$sga_dynamic_component shows us the current sizes of all these components in SGA. If we sum them up (172 + 4 + 4 + 292 = 472) is the total size where auto tuning is suppose to happen. Rest of 28m is for other areas in SGA like log buffer, keep/recycle cache sizes and any db_nk_cache_sizes if configured. Now lets open another console and connect with a normal oracle user to put the ASMM to the test.

 /*  This is another console where we login with user scott. The sysdba session is still intact in the other console. */

$ sqlplus scott/tiger@mydb
 SQL> create or replace package myPack
  2     is
  3      TYPE myType is table of char(2000) index by binary_integer;
  4      myTable myType;
  5     end;
  6       /
 Package created.

 SQL> begin
  2    for i in 1..100000 loop
  3       myPack.myTable(i) := i; 
  4    end loop;
  5  end;
  6  /
 PL/SQL procedure successfully completed.
 SQL> exit 

I established this session using shared server mode, so any variables I declare will be stored in the Large Pool where my UGA is being maintained. I created a packaged PL/SQL table of type char(2000) and inserted 100000 records in it. Being char(2000) each element's size is 2000 bytes no matter hat I assign to it. Hence after the population of the PL/SQL it is going to be around 200m(200*100000 bytes) in size. Since it is a packaged variable, so it is gonna stay in my Large Pool until I exit out the session. Once I exit from the session the variable should be cleaned out releasing space from Larg Pool.

/* Now we are back to the sysdba session */

SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
 COMPONENT                             SIZE_MB
---------------------------------- ----------
shared pool                               132
large pool                                232
java pool                                   4
streams pool                                0
DEFAULT buffer cache                      104
Notice the current size of all these pools. It is clear now that when Large Pool needed space Oracle squeezed both buffer cache and shared pool and gave required space to large pool. Also notice (132 + 232 + 4 + 104 = 472). Now these sizes will stay like this until any pool needs more memory then it has and that is when these sizes will adjust again.
Lets go back to our normal session and create a heavy work load on Default buffer cache

/* Open another console and connect with a normal oracle user. The sysdba session is still open in the other console. */

$ sqlplus scott/tiger@mydb
SQL> create table big_table as select * from all_objects;
Table created.
SQL> insert into big_table select * from big_table;
40697 rows created.
SQL> /
81394 rows created.
SQL> /
162788 rows created.
SQL> /
325576 rows created.
SQL> /
651152 rows created.
SQL> /
1302304 rows created.
SQL> commit;
Commit complete.
SQL> analyze table big_table compute statistics;
Table analyzed.
SQL> select table_name , round(blocks*8192/1024/1024,2) size_mb
  2  from user_tables
  3  where table_name = 'BIG_TABLE';
TABLE_NAME                        SIZE_MB
------------------------------ ----------
BIG_TABLE                          283.21
SQL> 

We have processed about 1.3 million rows here in the table big_table which is 283m in size i.e quite larger then the whole buffer cache as its current size is 104m. When these queries are processed there is an extensive aging out and loading of rows from big_table in buffer cache causing high physical reads and low cache hit ratio. MMAN (memory manager) captures it and signals the adjustment in the size of buffer cache as there is a plenty of free space in Large Pool. Lets go back to sysdba session and see what are the current sizes of these components.

/* Now we are back to the sysdba session */

SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
COMPONENT                              SIZE_MB
----------------------------------- ----------
shared pool                                108
large pool                                   4
java pool                                    4
streams pool                                 0
DEFAULT buffer cache                       356

See the buffer cache is larger the all others. The free space has been taken away from Large Pool and even some from Shared Pool also to accommodate heavy workload on buffer cache. As the current component sizes stand (108 + 4 + 4 + 356 = 472) Oracle is managing then within the boundary of 472m.

The dynamically adjusted sizes are retained through instance shutdowns if you are using server parameter file (i.e. spfile).

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 1073741824 bytes
Fixed Size                  1223540 bytes
Variable Size             671089804 bytes
Database Buffers          394264576 bytes
Redo Buffers                7163904 bytes
Database mounted.
Database opened.

SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
COMPONENT                            SIZE_MB
--------------------------------- ----------
shared pool                              108
large pool                                 4
java pool                                  4
streams pool                               0
DEFAULT buffer cache                     356
Note:- Even after a shutdown and restart the components are of the same size. 

 SQL> SELECT
  2     a.ksppinm "Parameter",
  3     b.ksppstvl "Value"
  4  FROM
  5     x$ksppi a,
  6     x$ksppsv b
  7  WHERE
  8     a.indx = b.indx AND
  9     a.ksppinm LIKE '/_/_%' escape '/' AND
 10    (a.ksppinm LIKE '%db_cache_size%' 
 11     OR a.ksppinm LIKE '%pool_size%');
Parameter                      Value
------------------------------ ----------
__shared_pool_size             113246208
__large_pool_size              4194304
__java_pool_size               4194304
__streams_pool_size            0
__db_cache_size                373293056

How is this happening? Actually whenever these components size is changed it is updated in undocumented siblings of auto tuned parameters. On the instance start up these undocumented parameters are read and the components are allocated accordingly. The above query shows how to read undocumented oracle parameters and their values from tables x$ksppi (contains parameter names) and x$ksppsv (contains parameter values for the instance) owned by sys

Here are some other useful columns in the view v$sga_dynamic_components where you can see the Operations have been happening with these components along their minimum and maximum sizes.

SQL> set lines 10000
SQL> column component format a20
SQL> select component , round(current_size/1024/1024,2) size_mb, 
  2  LAST_OPER_TYPE, OPER_COUNT, MIN_SIZE, MAX_SIZE
  3  from v$sga_dynamic_components
  4  where component like '%pool' 
  5  OR component ='DEFAULT buffer cache';
COMPONENT            SIZE_MB LAST_OPER_TYP OPER_COUNT 
-------------------- ------- ------------- ---------- 
shared pool              112 GROW                   1  
large pool                 4 SHRINK               114
java pool                  4 STATIC                 0
streams pool               0 STATIC                 0
DEFAULT buffer cache     352 GROW                 115
SQL> 

A few more things to know about ASMM:

When the ASMM is switched all auto tuned parameter sizes are managed by the oracle it self and any sizes manually defined for these parameters are no more considered as the max size that component may have, rather it is considered as a minimum size for that component.

$ sqlplus / as sysdba
 SQL> show parameter db_cache_size
 NAME                       TYPE        VALUE
-------------------------- ----------- --------------------
db_cache_size              big integer 0
SQL> show parameter pool_size
NAME                       TYPE        VALUE
-------------------------- ----------- --------------------
global_context_pool_size   string
java_pool_size             big integer 0
large_pool_size            big integer 0
olap_page_pool_size        big integer 0
shared_pool_size           big integer 0
streams_pool_size          big integer 0
SQL>
Here , All auto tuned parameter values are set to 0, which means 0 is the minimum size for this component and this component can be re-sized to 0 to allow other components use all of its memory. If we change them as follows:

SQL> alter system set db_cache_size=100m scope=both;
System altered.
SQL> show parameter db_cache_size
NAME                       TYPE        VALUE
-------------------------- ----------- --------------------
db_cache_size              big integer 100M
SQL> 
This means 100m is the minimum size for the buffer cache, no matter what happens to the other components this 100m will never be taken away from the Default buffer cache. Manual settings of the auto tuned parameters is useful when you don't want one of components to suffer too much because of auto adjustments in sizes.

When setting the auto tuned parameters manually if you set a size larger then its current size and the increase in the size can be supported by shrinking other components then the change is done immediately.

SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
COMPONENT               SIZE_MB
-------------------- ----------
shared pool                 112
large pool                    4
java pool                     4
streams pool                  0
DEFAULT buffer cache        352
SQL> alter system set shared_pool_size = 120m scope=both;
System altered.
SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
COMPONENT               SIZE_MB
-------------------- ----------
shared pool                 120
large pool                    4
java pool                     4
streams pool                  0
DEFAULT buffer cache        344
We increased the size of shared pool from 112m to 120m and there was space in buffer cache that was available for allocating to shared pool so our change came into effect immediately. And when there is no space available to grow the pool to the size you specified, you will get an error like this.

SQL> alter system set shared_pool_size = 450m scope=both;
alter system set shared_pool_size = 450m scope=both
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified  value is invalid
ORA-04033: Insufficient memory to grow pool
SQL>
When an auto tuned parameters are set to a value lower then their current size then the change is not done immediately.

SQL> alter system set shared_pool_size = 100m scope=both;
System altered.
SQL> select component , round(current_size/1024/1024,2) size_mb
  2  from v$sga_dynamic_components
  3  where component like '%pool' 
  4  OR component ='DEFAULT buffer cache';
COMPONENT               SIZE_MB
-------------------- ----------
shared pool                 120
large pool                    4
java pool                     4
streams pool                  0
DEFAULT buffer cache        344
SQL> 
Now according to the workload if a situations comes where Oracle has to shrink shared pool then the shared will be squeezed to 100m and the shrinking will stop since 100m is the minimum value for shared pool.

If you disable the ASMM then all the components that are being managed by the auto tuning will be freezed at their current sizes and become static unless you enable the ASMM again.

SQL> alter system set sga_target=0 scope=both;
System altered.
SQL> show parameter db_cache_size
NAME                        TYPE        VALUE
--------------------------- ----------- ---------------------
db_cache_size               big integer 344M
SQL> show parameter pool_size
NAME                        TYPE        VALUE
--------------------------- ----------- ---------------------
global_context_pool_size    string
java_pool_size              big integer 4M
large_pool_size             big integer 4M
olap_page_pool_size         big integer 0
shared_pool_size            big integer 120M
streams_pool_size           big integer 0
SQL>