SGA Size Breakdown (vsgasize.sql)
What This Script Does
Section titled “What This Script Does”This script provides comprehensive SGA memory analysis by:
- Displaying high-level SGA component sizes from V$SGA
- Breaking down detailed memory allocation from V$SGASTAT
- Excluding free memory to focus on actual usage
- Computing total memory consumption
- Showing both summary and detailed views
Script
Section titled “Script”rem vsgasize.sqlremttitle 'SGA Information - Breakdown'remcol name format a30 heading 'NAME'col bytes format 9,999,999,990 heading 'BYTES'rembreak on reportcompute sum of bytes on reportremselect name, value bytes from v$sga order by name;remset embedded onremselect name, bytes from v$sgastat where name not in ( 'free memory' ) order by name;remset embedded off
SQL> @vsgasize.sql
Required Privileges
Section titled “Required Privileges”- SELECT on V$SGA
- SELECT on V$SGASTAT
Sample Output
Section titled “Sample Output”SGA Information - Breakdown
NAME BYTES------------------------------ -----------Database Buffers 1,073,741,824Fixed Size 8,944,472Redo Buffers 33,554,432Variable Size 419,430,400 -----------sum 1,535,670,728
NAME BYTES------------------------------ -----------ASH buffers 16,777,216KCLS trace buffer 524,288KGLH0 8,388,608KQR L PO 33,554,432buffer_cache 1,073,741,824db_block_buffers 1,073,741,824java pool 16,777,216large pool 33,554,432library cache 50,331,648log_buffer 33,554,432shared pool 134,217,728sql area 83,886,080
Key Output Sections
Section titled “Key Output Sections”V$SGA Summary
Section titled “V$SGA Summary”- Database Buffers: Buffer cache memory
- Fixed Size: Fixed SGA structures
- Redo Buffers: Log buffer memory
- Variable Size: Shared pool and other variable components
V$SGASTAT Details
Section titled “V$SGASTAT Details”- buffer_cache: Database buffer pool
- shared pool: Shared SQL and PL/SQL area
- large pool: Large pool for backup/restore operations
- java pool: Java virtual machine memory
- log_buffer: Redo log buffer
- sql area: SQL statement cache
- library cache: Parsed SQL and PL/SQL
Understanding SGA Components
Section titled “Understanding SGA Components”Buffer Cache
Section titled “Buffer Cache”- Purpose: Caches data blocks from disk
- Performance Impact: Larger cache reduces physical I/O
- Tuning: Monitor buffer hit ratios
Shared Pool
Section titled “Shared Pool”- Components: Library cache, data dictionary cache
- Purpose: Caches parsed SQL, PL/SQL, and metadata
- Performance Impact: Reduces parsing overhead
Large Pool
Section titled “Large Pool”- Purpose: Large memory allocations
- Uses: RMAN, parallel execution, shared servers
- Sizing: Based on backup and parallel requirements
Java Pool
Section titled “Java Pool”- Purpose: Java virtual machine in the database
- Uses: Java stored procedures, SQLJ
- Sizing: Based on Java usage
Memory Analysis
Section titled “Memory Analysis”Total SGA Usage
Section titled “Total SGA Usage”-- Calculate SGA utilizationSELECT ROUND(SUM(value)/1024/1024, 2) AS total_sga_mb, ROUND(SUM(value)/1024/1024/1024, 2) AS total_sga_gbFROM v$sga;
Component Ratios
Section titled “Component Ratios”-- Calculate component percentagesSELECT name, ROUND(value/1024/1024, 2) AS size_mb, ROUND(value / (SELECT SUM(value) FROM v$sga) * 100, 2) AS pct_of_totalFROM v$sgaORDER BY value DESC;
Common Use Cases
Section titled “Common Use Cases”-
Memory Tuning
- Analyze SGA component sizes
- Identify over/under-allocated areas
- Plan memory redistribution
-
Capacity Planning
- Monitor memory growth trends
- Plan for increased workload
- Size new environments
-
Performance Analysis
- Correlate memory allocation with performance
- Identify memory bottlenecks
- Validate tuning changes
Performance Indicators
Section titled “Performance Indicators”Buffer Cache Analysis
Section titled “Buffer Cache Analysis”- Hit Ratio: Should be >95% for OLTP
- Size: Typically 60-80% of SGA
- Free Buffers: Monitor V$BUFFER_POOL_STATISTICS
Shared Pool Analysis
Section titled “Shared Pool Analysis”- Library Cache: High reload ratio indicates sizing issues
- Dictionary Cache: Miss ratio should be <15%
- Free Memory: Check for fragmentation
Memory Tuning Recommendations
Section titled “Memory Tuning Recommendations”Automatic Memory Management
Section titled “Automatic Memory Management”-- Enable automatic memory managementALTER SYSTEM SET sga_target = 2G;ALTER SYSTEM SET memory_target = 4G;
-- Check automatic tuningSELECT component, current_size, min_size, max_sizeFROM v$memory_dynamic_components;
Manual Tuning
Section titled “Manual Tuning”-- Increase buffer cacheALTER SYSTEM SET db_cache_size = 1200M;
-- Increase shared poolALTER SYSTEM SET shared_pool_size = 200M;
-- Increase large poolALTER SYSTEM SET large_pool_size = 50M;
Monitoring Scripts
Section titled “Monitoring Scripts”Memory Resize Operations
Section titled “Memory Resize Operations”-- Check recent memory operationsSELECT component, oper_type, oper_mode, parameter, initial_size, target_size, final_size, start_time, end_timeFROM v$memory_resize_opsWHERE start_time > SYSDATE - 1ORDER BY start_time DESC;
SGA Advisor Recommendations
Section titled “SGA Advisor Recommendations”-- Check SGA advisor recommendationsSELECT sga_size, sga_size_factor, estd_db_time, estd_physical_readsFROM v$sga_target_adviceORDER BY sga_size;
Memory Pressure Indicators
Section titled “Memory Pressure Indicators”Low Memory Symptoms
Section titled “Low Memory Symptoms”- High library cache reload ratio
- Frequent aging out of shared pool objects
- Buffer cache hit ratio decline
- ORA-04031 errors (unable to allocate memory)
Diagnostic Queries
Section titled “Diagnostic Queries”-- Check for memory pressureSELECT pool, name, bytesFROM v$sgastatWHERE name = 'free memory'ORDER BY pool;
-- Library cache statisticsSELECT namespace, reloads, invalidationsFROM v$librarycacheWHERE reloads > 0 OR invalidations > 0;
Best Practices
Section titled “Best Practices”-
Sizing Guidelines
- Start with automatic memory management
- Monitor and adjust based on workload
- Reserve 10-20% for OS and other processes
-
Regular Monitoring
- Check memory utilization weekly
- Monitor for ORA-04031 errors
- Track component growth trends
-
Change Management
- Test memory changes in development
- Implement changes during maintenance windows
- Document baseline and changes