一、问题描述
使用普通用户登录数据库报ORA-04031错误
$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Mon Mar 25 09:14:59 2024
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> conn trui/oracle
Error accessing PRODUCT_USER_PROFILE
Warning: Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
ERROR:
ORA-04031: unable to allocate 3896 bytes of shared memory ("shared pool","BEGIN
DBMS_APPLICATION_INFO....","sga heap(1,0)","kglsim object batch")
Error accessing package DBMS_APPLICATION_INFO
SP2-0575: Use of Oracle SQL feature not in SQL92 Entry Level.
Connected.
二、原因分析
ORA-04031错误是Oracle数据库的内存分配错误,它表示在SGA(System Global Area)中无法分配足够的共享内存空间。
对于大多数应用来说,共享池的大小对于Oracle 性能来说都是很重要的。共享池中保存数据字典高速缓冲和完全解析或编译的的PL/SQL 块和SQL 语句。
当我们在共享池中试图分配大片的连续内存失败的时候,Oracle首先刷新池中当前没使用的所有对象,使空闲内存块合并。如果仍然没有足够大单个的大块内存满足请求,就会产生ORA-04031 错误。
所以,可以通过增加SGA的空间来解决ORA-04031 错误。
三、处理方法
查看当前内存参数
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 1536M
sga_target big integer 1536M
修改 sga_max_size 和 sga_target参数值(比当前值大),本例设为2G
SQL> alter system set sga_max_size=2G scope=spfile;
System altered.
SQL> alter system set sga_target=2G scope=spfile;
System altered.
重启数据库使修改值生效
sga_max_size是用来约束sga_target的,sga_max_size和sga_target是静态参数,修改时需加scope=spfile,修改后需要重启数据库生效。一般情况下sga_max_size=sga_target 。
SQL> shutdown immediate
SQL> startup
验证参数是否修改成功
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 1536M
sga_target big integer 1536M
四、附上实际处理过程记录
查看当前内存参数
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 1536M
sga_target big integer 1536M
修改 sga_max_size参数值(比当前值大),本例设为2G
SQL> alter system set sga_max_size=2G scope=spfile;
System altered.
修改 sga_target 参数时出现ORA-04031错误
SQL> alter system set sga_target=2G scope=spfile;
alter system set sga_target=2G scope=spfile
*
ERROR at line 1:
ORA-04031: unable to allocate 3896 bytes of shared memory ("shared pool","alter
system set sga_target=...","sga heap(1,0)","kglsim object batch")
尝试 shutdown immediate关闭数据库,依然是ORA-04031错误
SQL> shutdown immediate
ORA-00604: error occurred at recursive SQL level 1
ORA-04031: unable to allocate 3896 bytes of shared memory ("shared pool","select count(*) from reg$","sga heap(1,0)","kglsim object batch")
尝试刷新共享池后再次shutdown,依然失败
SQL> alter system flush shared_pool ;
System altered.
SQL> shutdown immediate
ORA-00604: error occurred at recursive SQL level 1
ORA-04031: unable to allocate 3896 bytes of shared memory ("shared pool","select count(*) from reg$SQL>
使用shutdown abort关闭数据库
SQL> shutdown abort
ORACLE instance shut down.
启动数据库后查看当前参数值
SQL> startup
SQL> show parameter sga
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 2G
sga_target big integer 1536M