Grails控制器(简单)代码消耗太多内存。如何优化? [英] Grails controller (simple) code consuming too much memory. How to optimise?

查看:91
本文介绍了Grails控制器(简单)代码消耗太多内存。如何优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的域类和一个控制器,当运行时消耗超过400MB时 - 需要一些建议来优化它。



基本上我有两个域类A和B,它们在两个一对多的关系中(实际上B类表示A类实例之间的多对多关系),A的单个对象可以被多个B的对象引用为'fromA',而单个对象A可以被多个B的对象引用为'toA' - 就像GORM文档和航班和机场的例子 - A是机场,B是航班)。 A类是一个非常简单的领域类。

  class A {
static hasMany = [fromAs:B,toAs: B]
static mappedBy = [fromAs:fromA,toAs:toA]

int ...
String ...
//几个简单的字段
}

B类也很简单,它只是引用2中的A类

  class B {
A fromA
A toA

int ...
String ...
//几个简单字段
}

现在控制器所做的是将所有可能的航班信息填充到数据库中。它也会对int的一些非常小的计算作为这些int的设置,但为了使代码清晰,我正在跳过它。

  A.list()。each {tempFromA  - > 
A.list()。each {tempToA - >
def b = new B()
b.setFromA(tempFromA)
b.setToA(tempToA)
b.save(flush:false)
}

$ / code>

这就是所有的代码,它消耗超过400MB的内存。我试着每插入10 000条记录,都会调用hiernateSession.flush(),但总是在120 000条记录达到30万条记录的时候被提交,出现了内存不足的情况。



610数据库中的A,所以嵌入循环有超过370 000个周期(610x610)。

有关如何根据内存消耗优化代码的指导原则?它是如此简单的代码,可能我在这里做了一些非常愚蠢的事情......

解决方案

有一些非常好的GORM批处理 Ted Naleid撰写的这篇文章中的性能提示。特别是,Grails性能调整部分可能会有所帮助。


I have a pretty simple domain classes and a controller that while runtime consumes over 400MB - need some advice on optimising it.

Basically I have two domain classes A and B, which are in a two one-to-many ralations (actually class B represents a many-to-many relationship between class A instances, - single object of A can be referenced by multiple B's objects as 'fromA' and single object of A can be referenced by multiple B's objects as 'toA' - like the GORM documentation and flights and airports examples - A is an airport and B is a flight). Class A is a very simple domain class.

class A {
    static hasMany = [fromAs:B, toAs:B]
    static mappedBy = [fromAs:"fromA", toAs:"toA"]

    int ...
    String ...
    // several simple fields
}

Class B is also very simple, it is just referencing class A in 2 ways.

class B {
    A fromA
    A toA

    int ...
    String ...
    // several simple fields
}

Now what the controller does is to fill all the possible "flights" information to the database. It also does some really minor calculations of int's as setting of those int's but to make the code clear I'm skipping it.

A.list().each{ tempFromA ->
    A.list().each{ tempToA ->
        def b = new B()
            b.setFromA(tempFromA)
            b.setToA(tempToA)
            b.save(flush:false)
    }
}

That's all the code and it consumes over 400MB od RAM. I tried calling hiernateSession.flush() every 10 000 records inserted, but always after 120 000 up to 300 000 records get commited, the out of memory occures.

There are 610 A's in the database, so the embedded loops have over 370 000 cycles (610x610).

Any guidelines how to optimise this code in terms of memory consuptions? It is so simple code that probably I'm doing something really stupid here...

解决方案

There are some very good GORM batch performance tips in this article by Ted Naleid. In particular, the "Grails Performance Tweaks" section may help.

这篇关于Grails控制器(简单)代码消耗太多内存。如何优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆