Java request taking 40-50MB memory

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?

You can use a profiling tool to be certain. Also, you can use pagination if you don’t need all those 500 records.

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?

For Java profilers, check out this list of tools.

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.

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.

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. 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.

1 Like

Screenshot%20from%202018-03-30%2018%3A08%3A08

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 ?

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.

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

Thanks for helping.