Redian新闻
>
关于java执行SQL之后的内存问题?
avatar
关于java执行SQL之后的内存问题?# Java - 爪哇娇娃
w*e
1
我的一个server程序似乎总有内存泄漏,我用sun自己的profiling连续监控了2个礼拜
,heap size从40兆涨到了400兆,然后我看了内存的使用情况,发现有几项是数据库的
statement和resultset
percent live alloc'ed stack class
self accum bytes objs bytes objs trace name
10.76% 25.34% 52615656 243591 359835048 1665903 305016 com.mysql.jdbc.
ResultSet
10.17% 35.52% 49732488 230243 49732920 230245 305033 com.mysql.jdbc.
ResultSet
7.97% 53.57% 38974560 243591 38974560 243591 309126 com.mysql.jdbc.
Statement
7.53% 61.10% 36838880 230243 36838
avatar
A*o
2
you have to release jdbc resources manually...
like statement.close()
and, you have to deal with exception conditions
in short, jdbc sucks.

【在 w*******e 的大作中提到】
: 我的一个server程序似乎总有内存泄漏,我用sun自己的profiling连续监控了2个礼拜
: ,heap size从40兆涨到了400兆,然后我看了内存的使用情况,发现有几项是数据库的
: statement和resultset
: percent live alloc'ed stack class
: self accum bytes objs bytes objs trace name
: 10.76% 25.34% 52615656 243591 359835048 1665903 305016 com.mysql.jdbc.
: ResultSet
: 10.17% 35.52% 49732488 230243 49732920 230245 305033 com.mysql.jdbc.
: ResultSet
: 7.97% 53.57% 38974560 243591 38974560 243591 309126 com.mysql.jdbc.

avatar
m*t
3

No, "blindly copy and paste the code you find on google without
understanding how it works" sucks.
(no offense to either of you guys, I just couldn't help)

【在 A**o 的大作中提到】
: you have to release jdbc resources manually...
: like statement.close()
: and, you have to deal with exception conditions
: in short, jdbc sucks.

avatar
w*r
4
haha.. one blood see needle a

【在 m******t 的大作中提到】
:
: No, "blindly copy and paste the code you find on google without
: understanding how it works" sucks.
: (no offense to either of you guys, I just couldn't help)

avatar
A*o
5
np. but what can you do when you don't know nothing? quit coding?

【在 m******t 的大作中提到】
:
: No, "blindly copy and paste the code you find on google without
: understanding how it works" sucks.
: (no offense to either of you guys, I just couldn't help)

avatar
g*g
6
Bullshit like you are the best coder in the world.
And trust me, many managers are stupid enough.

【在 A**o 的大作中提到】
: np. but what can you do when you don't know nothing? quit coding?
avatar
w*e
7
说了这么多是不是就是大家都认为执行完executeQuery返回ResultSet以后,
在对ResultSet操作完并关闭ResultSet以后,一定还要手动关闭Statement,
否则就算它没有了reference,也会一直残存在内存中?

【在 g*****g 的大作中提到】
: Bullshit like you are the best coder in the world.
: And trust me, many managers are stupid enough.

avatar
A*o
8
try it first and see what happens.

【在 w*******e 的大作中提到】
: 说了这么多是不是就是大家都认为执行完executeQuery返回ResultSet以后,
: 在对ResultSet操作完并关闭ResultSet以后,一定还要手动关闭Statement,
: 否则就算它没有了reference,也会一直残存在内存中?

avatar
w*e
9
我试过了,执行10000条查询再profiling之后比较,确实不执行statement.close()的
话statement和ResultSet都占很大比例的内存,而且明显创建过10000个对象,结束的
时候内存也还存在10000个对象。要是每次都执行statement.close()或者总是同一个
statement就没有这个问题。不过同一个statement如果执行了下一个executeQuery那么
前一个命令中获得的ResultSet就会自动关闭.

【在 A**o 的大作中提到】
: try it first and see what happens.
avatar
g*g
10
Try the wrapper in spring to help you manage the resource.

【在 w*******e 的大作中提到】
: 我试过了,执行10000条查询再profiling之后比较,确实不执行statement.close()的
: 话statement和ResultSet都占很大比例的内存,而且明显创建过10000个对象,结束的
: 时候内存也还存在10000个对象。要是每次都执行statement.close()或者总是同一个
: statement就没有这个问题。不过同一个statement如果执行了下一个executeQuery那么
: 前一个命令中获得的ResultSet就会自动关闭.

avatar
m*t
11

I know, I know, "gotta bring bread to the table for kids..."

【在 A**o 的大作中提到】
: np. but what can you do when you don't know nothing? quit coding?
avatar
m*t
12

Well, mean comments aside, 8-), this is an interesting topic. My
understanding is this varies with databases. For instance, I know Statements
against Oracle need to be explicitly closed, OTOH SQL Server is more
lenient on this.
I normally just play safe and close all of them explicitly.

【在 w*******e 的大作中提到】
: 说了这么多是不是就是大家都认为执行完executeQuery返回ResultSet以后,
: 在对ResultSet操作完并关闭ResultSet以后,一定还要手动关闭Statement,
: 否则就算它没有了reference,也会一直残存在内存中?

avatar
t*k
13
我最怕statement resultrest connection没有被释放
所以我都是把resultset的结果存到collection里返回去的
返回前close所有能Close的东西
处理可能会慢点
但资源也有限啊
不知和我同样做法的有多少

【在 w*******e 的大作中提到】
: 我的一个server程序似乎总有内存泄漏,我用sun自己的profiling连续监控了2个礼拜
: ,heap size从40兆涨到了400兆,然后我看了内存的使用情况,发现有几项是数据库的
: statement和resultset
: percent live alloc'ed stack class
: self accum bytes objs bytes objs trace name
: 10.76% 25.34% 52615656 243591 359835048 1665903 305016 com.mysql.jdbc.
: ResultSet
: 10.17% 35.52% 49732488 230243 49732920 230245 305033 com.mysql.jdbc.
: ResultSet
: 7.97% 53.57% 38974560 243591 38974560 243591 309126 com.mysql.jdbc.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。