0

unused @Repository class will cause memory consumption

Our production application server go down very frequently and we tried analyzing the heap dump using eclipse memory analyzer tool.

Found that some char [] objects are taking more memory.

so for this issue we analyzed our application code and found that one @Repository class which was unused.

Will this unused @Repository class may cause this memory issue ?

import org.springframework.stereotype.Repository;


@Repository
public class BenchCommentDAOImpl extends BaseDAOImpl<BenchComment> implements BenchCommentDAO {

    
}

update 1

@Entity
@Table(name = "bnch_cmnt")
public class BenchComment extends BaseCrudEntity<Integer> implements Serializable {
     private static final long serialVersionUID = 1L;

     @Id
     @Column(name = "cmnt_id")
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Integer id;
     
     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "bnch_id", referencedColumnName = "bnch_id")
     @JsonBackReference
     private Bench bench;

     @Column(name = "bnch_id" , insertable=false ,updatable=false)
     private Integer benchId;

     @Column(name = "xtrnl_cmnt")
     private String comment;

}

@Repository class is extended from BenchComment class which is having comment as string field.

Hence in the heap dump analyzer am seeing some values with are matching with this comment field. That is where my doubt happening.

Update 2

@JsonManagedReference
@OneToMany(mappedBy="bench",cascade=CascadeType.ALL,fetch=FetchType.EAGER,orphanRemoval=true)
     private List<BenchComment> benchComments;

We do have OneToMany relationship with FetchType.EAGER for BenchComment pojo class. Will this implementation cause this heap leaks issue.?

Karthikeyan
  • 1,927
  • 6
  • 44
  • 109

1 Answers1

0

I don't think that your memory-leaks have anything to do with this empty repository.
In other projects, I saw unused repositories, but I never heard of any memory leaks.

Maybe there some other possible things:

  1. Eager-Relations
    Two objects, which are connected (e.g. with @JoinColumn) can cause an OutOfMemoryException if they fetch themselves in EAGER-mode.
    (They will fetch each other recursively until your RAM is empty)

  2. java.lang.OutOfMemoryException: java heap space

  3. Detecting out of memory errors

The empty repository doesn't mean, that your object never gets fetched from the database. If you have relations between your objects, then they may appear sometimes in your HEAP.

If you can find the cause, then we will be happy, when you posting it here.

Edit

To your edit with FetchType.EAGER: Yes it is your problem!
In Bench you are fetching the BenchComments eagerly and in BenchComments you are fechting the Bench eagerly. A vicious circle.

Just remove the EAGER-fetch-types and you should be fine.

akop
  • 5,981
  • 6
  • 24
  • 51
  • I have updated my question. We having `OneToMany` relationship. will that cause this issue.? – Karthikeyan Feb 24 '21 at 08:24
  • The default-fetch-type of `OneToMany` is `LAZY` - this should be fine. Maybe you are using some others (https://stackoverflow.com/questions/26601032/default-fetch-type-for-one-to-one-many-to-one-and-one-to-many-in-hibernate/42168093) – akop Feb 24 '21 at 08:29
  • Did you wrote a `.toString()`-method (or something familiar) which are using these lazy-fields? (I saw this before, but it results in a `StackOverflowError`) – akop Feb 24 '21 at 08:32
  • instead of using `fetch=FetchType.EAGER` do i need to change it as `fetch=FetchType.LAZY` ? – Karthikeyan Feb 24 '21 at 10:15
  • Yes, or just remove it. The default-types should be fine. – akop Feb 24 '21 at 10:16
  • Thanks.. let me try and update the outcome on this. – Karthikeyan Feb 24 '21 at 10:19
  • Still our implementation is not done.. i will definitly keep posted on the same once i complete my implementation and testing. I feel then its meaningful – Karthikeyan Mar 03 '21 at 13:44