Announcement Announcement Module
No announcement yet.
Memory issues with jpa and spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Memory issues with jpa and spring


    I am trying to understand why my app loads 24 instances (this is what a jmap histogram shows) of my "Sculpture" class whereas I only have 6 sculpture in my database.

    Here is the method I use in order to load my "Sculpture" instances:
     public List<Sculpture> findAllSculptures() {
            return entityManager.createNamedQuery("Sculpture.findAllSculptures").setHint("org.hibernate.cacheable", true).getResultList();
      public Sculpture findBySculptureID(Integer sculptureId) {
            return (Sculpture) entityManager.createNamedQuery("Sculpture.findBySculptureID").setHint("org.hibernate.cacheable", true).setParameter("sculptureID", sculptureId).getSingleResult();
       public List<Sculpture> findSculpturesByIds(List<Integer> sculpturesIds) {
            return entityManager.createNamedQuery("Sculpture.findSculpturesByIds").setHint("org.hibernate.cacheable", false).setParameter("sculpturesIds", sculpturesIds).getResultList();
    Here is the relevant portion of XML spring configuration code:

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
            <property name="persistenceUnitName" value="jbmPU" />
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="${database.showSql}" />
                    <property name="databasePlatform" value="${database.dialect}"/>
    Can anyone please help?

    Thanks in advance,


  • #2
    And why should that be an issue?! Each query goes to the database and creates/retrieves new instances of the objects in the database. Regardless if you have 1 or 200 objects.

    If 2 threads do a query for all objects you would have, in your case, 12 instances of your class.


    • #3
      Thanks Marten,
      The reason I ask is that my app is consuming huge amounts of memory and I would like to have more control over that.
      Is there not a way to make sure that only n instances are loaded (if there are n lines in the database.)?


      • #4
        No... Which is logical you don't want me and you operate on the same instance, hibernate will solve that concurrency issue... If your app is consuming amounts of memory I suggest looking somewhere else as in general this isn't an issue...


        • #5
          The reason I asked the first question is that I suspect there might be something wrong in my code or configuration and I need some advice on where to look to.

          Also, my instances are not meant to be altered or changed in any way. So why do I need several instances??

          Do you see my point?

          Last edited by balteo; Nov 24th, 2010, 07:16 AM.


          • #6
            No I still don't see your point as it isn't a problem. As I also stated if you have memory issues you probably have something else wrongly configured or doing something wrong in your application. However as you don't post code and/or configuration that is quite hard to guess.

            If you do post please use [ code][/code ] tags!