les 14 memory

35
Copyright © 2009, Oracle. All rights reserved. Managing Memory

Upload: denfil

Post on 27-Oct-2014

15 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.

Managing Memory

Page 2: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 2

Objectives

After completing this lesson, you should be able to:• Describe the memory components in the SGA• Implement Automatic Memory Management• Manually configure SGA parameters• Configure automatic PGA memory management

Page 3: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 3

Memory Management: Overview

DBAs must consider memory management to be a crucial part of their job because:• There is a finite amount of memory available• Allocating more memory to serve certain types of functions

can improve overall performance• Automatically tuned memory allocation is often the

appropriate configuration, but specific environments or even short-term conditions may require further attention

Page 4: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 4

Oracle Memory Structures

SGA

Java pool Databasebuffer cache

Shared pool

Redobuffer

Large poolStreams pool

Serverprocess

1PGA

Serverprocess

2PGA

Back-groundprocess

PGA

Keep buffer cache

Recycle buffer cache

nK block sizebuffer caches

Page 5: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 6

Buffer Cache

Data files

SGACheckpoint

queue

....

DBWn

Server

DB_BLOCK_SIZE

DB_CACHE_SIZE

DB_RECYCLE_CACHE_SIZE

DB_KEEP_CACHE_SIZE

LRU lists

....

DB buffer cache

Page 6: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 8

Using Multiple Buffer Pools

SGADB buffer caches

Keep pool

Recycle pool

Default pool

Page 7: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 9

Using Multiple Buffer Pools

CREATE INDEX cust_idx …STORAGE (BUFFER_POOL KEEP …);

ALTER TABLE oe.customersSTORAGE (BUFFER_POOL RECYCLE);

ALTER INDEX oe.cust_lname_ixSTORAGE (BUFFER_POOL KEEP);

Page 8: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 10

Shared pool

Library cache

Data dictionary cache

UGA

Shared poolShared pool

Shared Pool

• Size is specified by using SHARED_POOL_SIZE.• Library cache contains statement text, parsed code, and

execution plan.• Data dictionary cache contains definitions for tables,

columns, and privileges from the data dictionary tables.• The User Global Area (UGA) contains session information if

using Oracle shared server.

Result cache

Page 9: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 11

Shared poolDatabase

buffer cacheRedo log

buffer

Large pool

Large Pool

• Can be configured as a separate memory area in the SGA• Is sized by the LARGE_POOL_SIZE parameter• Is used to store data in memory for:

– UGA– Backup and restore operations– Parallel query messaging

Library cache

Dictionary cache

UGA Backup

Session

Parallel

Page 10: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 12

Java Pool

• Can be configured as a separate memory area in the SGA• Is sized by the JAVA_POOL_SIZE parameter • Is used to store data in memory for all session-specific Java

code and data within the JVM

Shared poolDatabase

buffer cacheRedo log

bufferLarge poolLibrary cache

Dictionary cache Java pool

Page 11: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 13

Redo Log Buffer

SQL> UPDATE employees

2 SET salary=salary*1.1

3 WHERE employee_id=736;

LGWR

Control files

ARCn

Archivedlog filesRedo log files

Data files

Shared poolRedo logbuffer

Database buffer cache Library cache

Dictionary cache

Serverprocess

Page 12: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 14

Automatic Shared Memory Management: Overview

• Automatically adapts to workload changes• Maximizes memory utilization• Helps eliminate out-of-memory errors

Online users Batch jobs

Buffer cache

Large pool

Shared pool

Java poolStreams pool

Buffer cache

Large pool

Shared pool

Java poolStreams pool

Example:

Page 13: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 15

How ASMM Works

• ASMM is based on workload information that MMON captures in the background.

• MMON uses memory advisors.• Memory is moved to where it is needed the most by MMAN.• If an SPFILE is used (which is recommended):

– Component sizes are saved across shutdowns– Saved values are used to bootstrap component sizes– There is no need to relearn optimal values

Page 14: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 16

Enabling Automatic Shared Memory Management

To enable ASMM from manual shared memory management:1. Get a value for SGA_TARGET:

2. Use that value to set SGA_TARGET.3. Set the values of the automatically sized SGA components

to 0. To switch to ASMM from Automatic Memory Management:1. Set the MEMORY_TARGET initialization parameter to 0.2. Set the values of the automatically sized SGA components

to 0.

SELECT ((SELECT SUM(value) FROM V$SGA) - (SELECT CURRENT_SIZE FROM V$SGA_DYNAMIC_FREE_MEMORY)) "SGA_TARGET" FROM DUAL;

Page 15: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 17

Behavior of AutotunedSGA Parameters

• When SGA_TARGET is not set or is set to 0:– Autotuned parameters specify the actual size of those

components– SHARED_POOL_SIZE may need to be increased

• When SGA_TARGET is set to a nonzero value:– Default value of autotuned parameters is 0– The specified value is used as a minimum size

SELECT SUM(bytes)/1024/1024 size_mb FROM v$sgastat WHERE pool = 'shared pool';

SELECT component, current_size/1024/1024 size_mb FROM v$sga_dynamic_components;

Page 16: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 18

Behavior of Manually Tuned SGA Parameters

• Some components are not autotuned.– DB_KEEP_CACHE_SIZE and DB_RECYCLE_CACHE_SIZE– Nonstandard block size buffer caches, DB_nK_CACHE_SIZE– LOG_BUFFER

• These components must be manually configured using database parameters.

• The memory used by these components reduces the amount of memory available for autotuning the SGA.

Page 17: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 19

Using the V$PARAMETER View

SGA_TARGET = 8G

SELECT name, value, isdefault FROM v$parameterWHERE name LIKE '%size';

DB_CACHE_SIZE = 0JAVA_POOL_SIZE = 0LARGE_POOL_SIZE = 0SHARED_POOL_SIZE = 0STREAMS_POOL_SIZE = 0

Page 18: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 20

Modifying the SGA_TARGET Parameter

• The SGA_TARGET initialization parameter:– Is dynamic– Can be increased up to SGA_MAX_SIZE– Can be reduced until all components reach their minimum

size• A change in the value of SGA_TARGET affects only

automatically sized components.

Page 19: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 21

SGA size = 8 GB

Disabling ASMM

• Setting SGA_TARGET to 0 disables autotuning.• Autotuned parameters are set to their current sizes.• The SGA size as a whole is unaffected.

Parameters:sga_target = 8G

shared_pool_size = 1G

Parameters:sga_target = 0

db_cache_size = 5G

shared_pool_size = 2G

large_pool_size = 512M

java_pool_size = 256M

streams_pool_size = 256M

SGA size = 8 GB

Original values

Page 20: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 22

Manually Resizing Dynamic SGA Parameters

• For autotuned parameters, manual resizing:– Results in immediate component resize if the new value is

greater than the current size– Changes the minimum size if the new value is smaller than

the current size• Resizing manually tuned parameters affects only the tunable

portion of the SGA.

Page 21: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 23

Using the Memory Advisor toSize the SGA

Page 22: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 24

Program Global Area (PGA)

PGA

Serverprocess PGA

Shared poolor

large poolPGA

Dedicatedconnections

Sharedserver PGA

Shared serverconnections

PrivateSQL

areas

Cursor and SQL

area

Sessionmemory

Workarea

Page 23: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 26

Automatic PGA Memory Management

• Dynamically adjusts the amount of PGA memory dedicated to work areas, on the basis of the PGA_AGGREGATE_TARGET parameter

• Helps maximize the performance of all the memory-intensive SQL operations

• Is enabled by default

Page 24: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 27

PGA Management Resources

• Statistics to manage the PGA_AGGREGATE_TARGETinitialization parameter, such as PGA cache hit percentage

• Statistics on allocation and use of work area memory can be viewed in the following dynamic performance views:

– V$SYSSTAT

– V$SESSTAT

– V$PGASTAT

– V$SQL_WORKAREA

– V$SQL_WORKAREA_ACTIVE

• Views to assist in sizing the PGA work area are:– V$PGA_TARGET_ADVICE

– V$PGA_TARGET_ADVICE_HISTOGRAM/V$SQL_WORKAREA_HISTOGRAM

Page 25: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 28

Using the Memory Advisorto Size the PGA

Page 26: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 29

Automatic Memory Management: Overview

With Automatic Memory Management, the database can size the SGA and PGA automatically according to your workload.

11g

Memory Target

MemoryMax Target

250 MB

350 MB

ALTER SYSTEM SET MEMORY_TARGET=300M;

11g

Memory Target

MemoryMax Target

300 MB

350 MB

Page 27: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 30

Oracle Database Memory Parameters

DB_KEEP_CACHE_SIZEDB_RECYCLE_CACHE_SIZE

DB_nK_CACHE_SIZERESULT_CACHE_MAX_SIZE

LOG_BUFFER

SHARED_POOL_SIZEDB_CACHE_SIZELARGE_POOL_SIZEJAVA_POOL_SIZESTREAMS_POOL_SIZE

SGA_TARGET

SGA_MAX_SIZEMEMORY_MAX_TARGET

MEMORY_TARGET

Others

PGA_AGGREGATE_TARGET

Page 28: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 31

Automatic Memory Parameter Dependency

ST+PAT<=MT<=MMT

MT>0

ST>0 & PAT>0

ST>0 & PAT=0 PAT=MT-ST

ST=0 & PAT>0 ST=min(MT-PAT,SMS)

ST=60%MTPAT=40%MT

MMT>0MT=0

MMT=MTMMT=0

MT=0

Y YN

N

Y

Both SGA and PGA can grow and shrink automatically.

ST>0SGA & PGA

are separatelyautotuned

Y

Only PGAis auto tuned

N

MT can be dynamically

changed later.

SGA and PGA cannotgrow and shrink automatically.

Minimum possible valuesN

N

N

Y

Y

Y

N

Page 29: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 33

Enabling Automatic Memory Management with EM

Page 30: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 34

Monitor Automatic Memory Management

1

2

Page 31: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 35

Monitoring Automatic Memory Management

If you want to monitor the decisions made by Automatic Memory Management via a command line:• V$MEMORY_DYNAMIC_COMPONENTS has the current status

of all memory components• V$MEMORY_RESIZE_OPS has a circular history buffer of the

last 800 memory resize requests• V$MEMORY_TARGET_ADVICE provides tuning advice for the MEMORY_TARGET initialization parameter

Page 32: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 36

Efficient Memory Usage: Guidelines

• Try to fit the SGA into physical memory.• Tune for a high buffer cache hit ratio, with the following

caveats:– Even valid and necessary full table scans lower it.– It is possible that unnecessary repeated reads of the same

blocks are artificially raising it.• Use the Memory Advisors.

Page 33: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 38

Memory Tuning Guidelinesfor the Library Cache

• Establish formatting conventions for developers so that SQL statements match in the cache.

• Use bind variables.• Eliminate unnecessary duplicate SQL.• Consider using CURSOR_SHARING.• Use PL/SQL when possible.• Cache sequence numbers.• Pin objects in the library cache.

Page 34: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 40

Summary

In this lesson, you should have learned how to:• Describe the memory components in the SGA• Implement Automatic Memory Management• Manually configure SGA parameters• Use automatic PGA memory management

Page 35: Les 14 Memory

Copyright © 2009, Oracle. All rights reserved.14 - 41

Practice 14 Overview: Using AMM to Correct a Memory Allocation Problem

This practice covers the following topics:• Diagnosing a memory allocation problem• Enabling and implementing Automatic Memory Management