# Java request taking 40-50MB memory

**URL:** <https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453>\
**Category:** Hibernate ORM\
**Created:** [March 29, 2018, 3:55am UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453 "2018-03-29T03:55:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit\_bansal](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@ankit\_bansal](https://discourse.hibernate.org/u/ankit_bansal)\
**Post date:** [March 29, 2018, 3:55am UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/1 "2018-03-29T03:55:17Z")

</div>

I am using spring boot with JPA Hibernate.  
I am monitoring the service for Heap and found that each of my request is taking around 40-50 MB.

So the memory gets increased, after few requests GC runs, it frees the memory and this goes on forever.

So my first question is that is this memory leak?

Also I am trying to find what is causing this. So, I have used Runtime.getRuntime() freeMemory and totalMemory() to identify that around 15MB is getting used when getting one db call and populating projection with it

```
public interface RecommendationProjection {

    public String getType();
    public boolean getIsOK();
    public int getId();
    public int getTagCount();
    public double getQuality() ;
    public LocalDateTime getLastActivity();

}

```

and hibernate returns 567 records, so basically what I am getting from DB is list of 567 above projection, But what i dont understand that how could this object take such high memory? Is hibernate causing this?

When using projection, hibernate queries for specific field or fetches all fields from database?

Then I am mapping this domain to DTO, which again uses 15-20MB memory?  
this is my DTO

```
public class RecommendationInfoDTO {

    private String type;
    private boolean isOK;
    private int id;
    private int tagCount;
    private double quality ;
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone="IST")
    private LocalDateTime lastActivity;

    .. getters and setters
}

```

FYI : For monitoring I am using **VisualVM**.

Also, is there any tool that can help in such cases?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 29, 2018, 3:58am UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/2 "2018-03-29T03:58:12Z")

</div>

You can use a profiling tool to be certain. Also, you can [use pagination](https://vladmihalcea.com/whats-new-in-jpa-2-2-stream-the-result-of-a-query-execution/) if you don’t need all those 500 records.

---

<div class="post-metadata">

**Author:** ![ankit\_bansal](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@ankit\_bansal](https://discourse.hibernate.org/u/ankit_bansal)\
**Post date:** [March 29, 2018, 1:14pm UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/3 "2018-03-29T13:14:03Z")

</div>

Which tool I can use?(I have used VisualVM) Also just to test whether actually hibernate / JPA, is taking memory, I have removed the query from JPA and get the same data(more than 500 rows) using SQL(simple JDBC Connection), the memory gets reduced to 3-4 MB.

So now I am sure that JPA is causing this memory bump. Also I have tried to fire the native query using JPA(with @Query and nativeQuery=true), same amount of memory is used(40MB).

Also I have tried to analyze the heap dump but could not get anything out of it as all my DT0 / Objects are using very less memory?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 29, 2018, 1:21pm UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/4 "2018-03-29T13:21:51Z")

</div>

For Java profilers, check out [this list of tools](http://www.baeldung.com/java-profilers).

The profiler tool will tell you how much memory is spent per Object type, so you will know better what exactly is taking so muchc memory.

---

<div class="post-metadata">

**Author:** ![ankit\_bansal](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@ankit\_bansal](https://discourse.hibernate.org/u/ankit_bansal)\
**Post date:** [March 30, 2018, 10:31am UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/5 "2018-03-30T10:31:54Z")

</div>

Can you just clarify my one doubt :

When I am using projection and fetch data from hibernate, does hibernate first fetch all the data and then map with projection OR it just fetches the specific data.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 30, 2018, 11:31am UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/6 "2018-03-30T11:31:27Z")

</div>

> Does hibernate first fetch all the data and then map with projection OR it just fetches the specific data.

You should always [enable SQL statement logging when using JPA and Hibernate](https://vladmihalcea.com/the-best-way-to-log-jdbc-statements/). If you do so, you will always know what SQL query Hibernate executes for you.

Now, to answer your question. In a projection, only the properties you specify to be selected are going to be fetched. Check out [this article for more details about how to execute a DTO projection query](https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/).

---

<div class="post-metadata">

**Author:** ![ankit\_bansal](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@ankit\_bansal](https://discourse.hibernate.org/u/ankit_bansal)\
**Post date:** [March 30, 2018, 1:36pm UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/7 "2018-03-30T13:36:32Z")

</div>

![Screenshot%20from%202018-03-30%2018%3A08%3A08](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/hibernate/original/1X/2e64e0aaddb9d1f8f93831e2303a3c291036937f.png)

This is my heap dump diff. I amm unable to find anything which causes memory.

I am firing 6 hibernate queries in a request and 3 simple plain mysql queries(using jdbc call)  
Issue is just 1 hibernate call. I think some thing is wrong with my hibernate ?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 30, 2018, 1:37pm UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/8 "2018-03-30T13:37:48Z")

</div>

Heat dumps will not help you. You need request-based profiling to know exactly what a given thread allocates before and after running a query.

---

<div class="post-metadata">

**Author:** ![ankit\_bansal](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@ankit\_bansal](https://discourse.hibernate.org/u/ankit_bansal)\
**Post date:** [March 30, 2018, 3:56pm UTC](https://discourse.hibernate.org/t/java-request-taking-40-50mb-memory/453/9 "2018-03-30T15:56:15Z")

</div>

How can i do request based profiling? I mean are there any tools available which i can use?

Thanks for helping.
