- 浏览: 224288 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (344)
- Spring (38)
- Spring-mvc (9)
- hibernate (21)
- mvn (5)
- shiro (2)
- java script (14)
- jquery (9)
- ext (6)
- asm (1)
- 大数据 (2)
- 分布式 (14)
- node.js (1)
- webservice (4)
- java (47)
- oracle (36)
- mysql (11)
- sql (33)
- 工具 (13)
- github (3)
- tomcat (4)
- jetty (1)
- struts2 (1)
- android (13)
- xml (1)
- primefaces (1)
- secure (2)
- 编码 (3)
- EL (3)
- jsp (2)
- css (7)
- java 算法 (3)
- 移动端 (2)
- other (3)
- mybatis (1)
- 缓存 (1)
- 乱 (1)
- apache commons (2)
- ehcache (1)
- 方法论 (1)
- activiti (1)
- sqlserver (5)
- 业务 (4)
- 数据结构 (5)
- liux (1)
- commons (4)
- jvm (2)
最新评论
-
张yyyyyy:
不明觉厉
一次sql 调优心得 -
masuweng:
某一类日期范围内每7天一组的销量之和,以及行转列 -
faradayroger:
[color=green][color=red][/color ...
定时JOB,去请求数据,并找出最新数据持久化 -
cainiao1923:
java-lxm 写道 写文章贴个图也不好好贴....难道是网 ...
spring 注入static属性 -
java-lxm:
写文章贴个图也不好好贴
spring 注入static属性
原文:https://forum.hibernate.org/viewtopic.php?p=2386803
Hi, I've written a small test to measure the memory consumption of my Hibernate code. It seem that getting a List of objects with the list() method of a Query uses much more memory than creating the List manually by using a ScrollableResult.
The ScrollableResult memory consumption is almost identical to the consumption of a pure JDBC implementation. The list() memory consumption needs much more MB. In my case with my Database 50.000 Object need approximately 13 MB with ScrollableResult and 23 MB with list()!!
Can anybody explain this behaviour? Is my code wrong?
Hibernate Version: 3.2.2ga
Windows XP SP2
Database: Oracel 10g
Testcode:
Note: You have to replace the HQL Query with your Hibernate Objects!
Code:
public class HibernateMemoryConsumptionTest extends TestCase {
private int size = 50000;
private int repeats = 2;
private boolean trackMemoryConsumption = true;
private Runtime runtime;
protected void setUp() throws Exception {
super.setUp();
runtime = Runtime.getRuntime();
}
protected void tearDown() throws Exception {
super.tearDown();
runtime = null;
}
public void testHibernateWithOneSession() {
// init
HibernateUtil.getSessionFactory();
long heap1 = 0;
long heap2 = 0;
long start = System.currentTimeMillis();
StatelessSession session = null;
Transaction tx = null;
List results = null;
try {
session = HibernateUtil.getSessionFactory().openStatelessSession();
for (int i = 0; i < repeats; i++) {
if (trackMemoryConsumption) {
System.err
.println("Note: Memory measurement can influence time measurement!");
runGC();
heap1 = usedMemory(); // Take a before heap snapshot
}
tx = session.beginTransaction();
Query query = session
.createQuery("FROM Cashflow ");
query.setFirstResult(1);
query.setMaxResults(size);
results = query.list();
tx.commit();
assertEquals(size, results.size());
if (trackMemoryConsumption) {
heap2 = usedMemory(); // Take an after heap snapshot:
final long heapsizeDiff = heap2 - heap1;
System.out.println("'before' heap: " + heap1
+ ", 'after' heap: " + heap2);
System.out.println("heap delta: " + heapsizeDiff / 1024d
/ 1024d + " MB");
}
results = null;
runGC();// aufräumen für nächste schleife
}
} catch (Throwable t) {
t.printStackTrace();
if (tx != null) {
tx.rollback();
}
throw new PersistenceException(t);
}
long end = System.currentTimeMillis();
System.out.println("runtime with list() "
+ repeats + " repeats with " + size + " Cashflows = "
+ ((end - start) / (double) repeats));
}
public void testHibernateWithScrollableResult() {
// init wird nicht mitgezählt!
HibernateUtil.getSessionFactory();
long heap1 = 0;
long heap2 = 0;
long start = System.currentTimeMillis();
StatelessSession session = null;
Transaction tx = null;
ScrollableResults results = null;
List cashFlows = null;
try {
session = HibernateUtil.getSessionFactory().openStatelessSession();
for (int i = 0; i < repeats; i++) {
cashFlows = new ArrayList();
if (trackMemoryConsumption) {
System.err.println("Note: Memory measurement can influence time measurement!");
runGC();
heap1 = usedMemory(); // Take a before heap snapshot
}
tx = session.beginTransaction();
Query query = session
.createQuery("FROM Cashflow ");
query.setFirstResult(1);
query.setMaxResults(size);
results = query.scroll(ScrollMode.FORWARD_ONLY);
while (results.next()) {
cashFlows.add(results.get(0));
}
tx.commit();
assertEquals(size, cashFlows.size());
if (trackMemoryConsumption) {
heap2 = usedMemory(); // Take an after heap snapshot:
final long heapsizeDiff = heap2 - heap1;
System.out.println("'before' heap: " + heap1
+ ", 'after' heap: " + heap2);
System.out.println("heap delta: " + heapsizeDiff / 1024d
/ 1024d + " MB");
}
results = null;
cashFlows = null;
runGC();// aufräumen für nächste schleife
}
} catch (Throwable t) {
t.printStackTrace();
if (tx != null) {
tx.rollback();
}
throw new PersistenceException(t);
}
long end = System.currentTimeMillis();
System.out.println("runtime with ScrollableResults "
+ repeats + " repeats with " + size + " Cashflows = "
+ ((end - start) / (double) repeats));
}
private void runGC() throws Exception {
// It helps to call Runtime.gc()
// using several method calls:
for (int r = 0; r < 4; ++r)
_runGC();
}
private void _runGC() throws Exception {
long usedMem1 = usedMemory(), usedMem2 = Long.MAX_VALUE;
for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {
runtime.runFinalization();
runtime.gc();
Thread.currentThread().yield();
usedMem2 = usedMem1;
usedMem1 = usedMemory();
}
}
private long usedMemory() {
return runtime.totalMemory() - runtime.freeMemory();
}
}
Hi, I've written a small test to measure the memory consumption of my Hibernate code. It seem that getting a List of objects with the list() method of a Query uses much more memory than creating the List manually by using a ScrollableResult.
The ScrollableResult memory consumption is almost identical to the consumption of a pure JDBC implementation. The list() memory consumption needs much more MB. In my case with my Database 50.000 Object need approximately 13 MB with ScrollableResult and 23 MB with list()!!
Can anybody explain this behaviour? Is my code wrong?
Hibernate Version: 3.2.2ga
Windows XP SP2
Database: Oracel 10g
Testcode:
Note: You have to replace the HQL Query with your Hibernate Objects!
Code:
public class HibernateMemoryConsumptionTest extends TestCase {
private int size = 50000;
private int repeats = 2;
private boolean trackMemoryConsumption = true;
private Runtime runtime;
protected void setUp() throws Exception {
super.setUp();
runtime = Runtime.getRuntime();
}
protected void tearDown() throws Exception {
super.tearDown();
runtime = null;
}
public void testHibernateWithOneSession() {
// init
HibernateUtil.getSessionFactory();
long heap1 = 0;
long heap2 = 0;
long start = System.currentTimeMillis();
StatelessSession session = null;
Transaction tx = null;
List results = null;
try {
session = HibernateUtil.getSessionFactory().openStatelessSession();
for (int i = 0; i < repeats; i++) {
if (trackMemoryConsumption) {
System.err
.println("Note: Memory measurement can influence time measurement!");
runGC();
heap1 = usedMemory(); // Take a before heap snapshot
}
tx = session.beginTransaction();
Query query = session
.createQuery("FROM Cashflow ");
query.setFirstResult(1);
query.setMaxResults(size);
results = query.list();
tx.commit();
assertEquals(size, results.size());
if (trackMemoryConsumption) {
heap2 = usedMemory(); // Take an after heap snapshot:
final long heapsizeDiff = heap2 - heap1;
System.out.println("'before' heap: " + heap1
+ ", 'after' heap: " + heap2);
System.out.println("heap delta: " + heapsizeDiff / 1024d
/ 1024d + " MB");
}
results = null;
runGC();// aufräumen für nächste schleife
}
} catch (Throwable t) {
t.printStackTrace();
if (tx != null) {
tx.rollback();
}
throw new PersistenceException(t);
}
long end = System.currentTimeMillis();
System.out.println("runtime with list() "
+ repeats + " repeats with " + size + " Cashflows = "
+ ((end - start) / (double) repeats));
}
public void testHibernateWithScrollableResult() {
// init wird nicht mitgezählt!
HibernateUtil.getSessionFactory();
long heap1 = 0;
long heap2 = 0;
long start = System.currentTimeMillis();
StatelessSession session = null;
Transaction tx = null;
ScrollableResults results = null;
List cashFlows = null;
try {
session = HibernateUtil.getSessionFactory().openStatelessSession();
for (int i = 0; i < repeats; i++) {
cashFlows = new ArrayList();
if (trackMemoryConsumption) {
System.err.println("Note: Memory measurement can influence time measurement!");
runGC();
heap1 = usedMemory(); // Take a before heap snapshot
}
tx = session.beginTransaction();
Query query = session
.createQuery("FROM Cashflow ");
query.setFirstResult(1);
query.setMaxResults(size);
results = query.scroll(ScrollMode.FORWARD_ONLY);
while (results.next()) {
cashFlows.add(results.get(0));
}
tx.commit();
assertEquals(size, cashFlows.size());
if (trackMemoryConsumption) {
heap2 = usedMemory(); // Take an after heap snapshot:
final long heapsizeDiff = heap2 - heap1;
System.out.println("'before' heap: " + heap1
+ ", 'after' heap: " + heap2);
System.out.println("heap delta: " + heapsizeDiff / 1024d
/ 1024d + " MB");
}
results = null;
cashFlows = null;
runGC();// aufräumen für nächste schleife
}
} catch (Throwable t) {
t.printStackTrace();
if (tx != null) {
tx.rollback();
}
throw new PersistenceException(t);
}
long end = System.currentTimeMillis();
System.out.println("runtime with ScrollableResults "
+ repeats + " repeats with " + size + " Cashflows = "
+ ((end - start) / (double) repeats));
}
private void runGC() throws Exception {
// It helps to call Runtime.gc()
// using several method calls:
for (int r = 0; r < 4; ++r)
_runGC();
}
private void _runGC() throws Exception {
long usedMem1 = usedMemory(), usedMem2 = Long.MAX_VALUE;
for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {
runtime.runFinalization();
runtime.gc();
Thread.currentThread().yield();
usedMem2 = usedMem1;
usedMem1 = usedMemory();
}
}
private long usedMemory() {
return runtime.totalMemory() - runtime.freeMemory();
}
}
发表评论
-
更新的想法
2017-04-25 17:58 494全部更新的话,可以query-copy-delete-in ... -
hibernate 查询VO
2017-03-02 15:03 741需求很简单,就是关联两个实体,多查询一个name出来。这样就 ... -
(转)对The method createBlob() is undefined for the type Hibernate异常的解决办法
2017-02-11 08:44 1166REFS:http://blog.csdn.net/zjw ... -
(转)Hibernate 3保存BOLB的方法,mysql(Hibernate4不适合此法)
2017-02-11 08:40 449Tutorial:Saving/Retreving BLO ... -
(转)Java数据类型和MySql数据类型对应表
2017-02-10 14:13 841java mysql 数据类型对照 类型名称 ... -
(转)hibernate sqlQuery mysql别名问题
2017-02-10 14:11 613项目环境:hibernate3.6.1 mysql5.7 ... -
AliasToBeanResultTransformer 。问题:Could not find setter for 0 on class MyClass
2017-02-10 14:05 1091As you are using AliasToBeanR ... -
mysq 的decimal类型与hibernate 的 setResultTransformer
2017-02-10 12:06 792mysql数据库里是decimal类型的话(powerd ... -
同名方法参数存在继承关系时候,调用哪个的疑问。{系列基础不确定问题}
2017-02-07 11:51 318private void setResultTransfo ... -
(转)Path expected for join!错误处理
2017-02-07 11:43 1071在hibernate中,进行连接查询时,如果使用join语句 ... -
(转)hibernate中的addEntity setResultTransformer的比较
2017-02-07 11:38 694如果使用原生sql语句进行query查询时,hibe ... -
(转) hibernate中使用数据库关键字(保留字)
2016-12-20 11:12 1229在hibernate中,当一个实体对象使用了数据库保留 ... -
(转)hibernate 二级缓存配置
2016-12-14 11:58 385在applicationContext.xml文件中添 ... -
hql语句的from不支持子查询
2016-11-10 15:26 1085上午,跨表查询,写了很长的hql语句,就是出错!后来上网查查, ... -
hibernate PK生成策略总结
2016-11-09 10:12 637hibernate提供的主键生成策略,使我 ... -
表的同步、hibernate随想
2016-10-12 15:11 416两个相同表(A-》B)之间数据同步方案-- 1.直接删除B表 ... -
主键问题
2016-03-30 17:30 418问题: 附表用到主表的主键ID,在先persist主表之后, ... -
上传文件到数据库
2016-03-26 16:29 625在hibernate Annotation中,实体BLOB ... -
hibernate异常坑人
2015-06-23 14:36 821org.hibernate.QueryParameterEx ... -
Note that HQL subqueries can occur only in the select or where clauses.
2014-10-10 16:19 622Note that HQL subqueries can oc ...
相关推荐
6. **执行查询**:调用 list() 或 scroll() 方法执行查询,获取结果集。 7. **处理结果**:遍历查询结果,将数据绑定到视图或者进一步处理。 总结,"Hibernate 多表连接分页查询示范项目"是一个实用的示例,它展示...
本文将探讨Hibernate中两种主要的分页方式:`query.scroll()`和使用`query.setFirstResult(), query.setMaxResults()`。 首先,`query.scroll()`方法基于JDBC 2.0的可滚动结果集实现。这种方式允许应用程序在结果...
最后,通过`list()`或`scroll()`方法执行查询并获取结果集: ```java Query query = session.createQuery(hql); query.setParameter("value", someValue); query.setFirstResult(start); query.setMaxResults(count...
总的来说,Hibernate的Session接口提供了一套完整的对象持久化和数据库操作机制,使得开发者能够以面向对象的方式处理数据,极大地提高了开发效率和代码的可读性。通过深入理解和熟练运用Session,可以轻松地在Java...
### 在JDBC与Hibernate中实现分页功能 随着数据量的不断增长,...通过上述介绍可以看出,无论是使用Hibernate还是JDBC,合理设计和实现分页功能能够显著提升用户界面的性能和响应速度,同时也提高了系统的整体性能。
Query接口提供了执行HQL查询的方法,如`createQuery()`, `list()`, `scroll()`, `executeUpdate()`等。 三、Hibernate注解 1. @Entity:标记一个类为实体类,对应数据库中的表。 2. @Table:定义实体对应的数据库...
Hibernate分页查询基于SQL的LIMIT和OFFSET子句,通过Session的createQuery或createSQLQuery方法创建查询,并设置FirstResult和MaxResults属性来实现分页。FirstResult表示从结果集的第几个元素开始获取,MaxResults...
- 结果集转换:`addEntity()`方法用于指定查询结果应被映射到哪个实体类,`list()`或`scroll()`方法执行查询并返回结果集,这些结果集将自动封装为对应的Java对象。 5. 示例: 假设我们有一个`User`实体类,对应...
本文将详细讲解如何在Hibernate中实现真分页(物理分页)和假分页(逻辑分页)。 首先,我们来了解什么是真分页和假分页。假分页,也称为内存分页,它一次性加载所有数据到内存中,然后通过索引进行分页显示,这种...
Hibernate 提供了 Criteria API 和 HQL(Hibernate Query Language)来实现分页查询。 2. Hibernate Criteria API 分页: Hibernate 的 Criteria API 允许开发者以面向对象的方式构建动态查询。要实现分页,可以...
本文将深入探讨Hibernate中的两个重要概念:一对多(One-to-Many)和多对一(Many-to-One)的持久化映射。 ### 一对多映射 **定义**: 一对多映射表示一个实体(如部门)可以关联多个实体(如员工)。在数据库层面...
在Hibernate中,通过Session对象的createQuery或createSQLQuery方法创建HQL查询,然后调用list、uniqueResult、scroll等方法执行查询。执行过程中,Hibernate会自动进行类型转换和结果集的封装,极大地降低了开发...
4. 调用`list()`或`scroll()`方法执行查询,返回结果集。 示例代码: ```java Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(YourEntity.class); criteria....
3. **检索**: `Session`的`get()`、`load()`方法获取单个实体,`list()`, `scroll()`, `iterate()`用于查询结果集。 4. **删除**: 使用`Session`的`delete()`方法删除实体。 5. **事务处理**: 在事务边界内执行...
Hibernate提供了SQL风格的Query接口,可以通过setFirstResult()和setMaxResults()方法实现分页。setFirstResult()设置从哪一条记录开始,setMaxResults()指定最多返回多少条记录。例如: ```java Session ...
4. **结果处理**:获取查询结果后,可以通过List、Scroll或EntityResult进行处理。 总结,Hibernate Search 4.1.0.CR2是Java开发中实现高效全文搜索的强大工具。通过深入理解和熟练运用其特性,开发者可以构建出...
在分页场景中,Hibernate提供了Criteria、HQL(Hibernate Query Language)以及Session的分页方法,如`scroll()`和`list()`,来实现数据的分页获取。 在分页实现过程中,我们需要考虑以下几个关键点: 1. **分页...
总的来说,Hibernate提供的分页技术使得开发者能够灵活地处理数据分页,同时考虑到性能和用户体验。在实际开发中,根据项目需求和数据规模,选择最适合的分页策略是非常重要的。通过熟练掌握和运用这些分页技术,...
在数据库操作中,有时会遇到一些异常情况,比如尝试对一个不支持滚动(scroll)或某些特定操作的结果集进行操作时,系统可能会抛出异常。本篇文章将针对此类问题提供详细的解决方案,并通过分析具体的异常堆栈信息来...
这可以通过在Hibernate配置文件中设置`hibernate.connection.autocommit`属性为`false`,并在查询前手动设置事务,然后通过`Session`的`createSQLQuery()`或`createQuery()`方法附加`ResultSet.TYPE_SCROLL_...