1

I am using Spring + Hibernate in my java web application. I read in the web that spring and hibernate will use more memory than usual. Frequently I am running out of memory. So I tried to profile my tomcat by using jvisualvm. When I see in memory, objects are keep on adding (not replacing existing objects).

For example: if I am loading a list of product objects (1000 objects in the list), then 1000 product objects will be added in heap memory. If I refresh the data grid again it is adding 1000 objects in heap memory, like that it is keep on increasing (see the image). is there a way to Replace existing objects in heap memory instead of keep on adding?

heap memory keep on adding prmaster objects

Roman C
  • 49,761
  • 33
  • 66
  • 176
vissu
  • 1,921
  • 7
  • 37
  • 52
  • Are you sure you are not misusing either API somewhat, like for instance forgetting to discard a result set or something similar? – fge Jun 17 '13 at 14:01
  • Yes, I am closing session every where. so that it will close result set also. – vissu Jun 17 '13 at 14:02
  • this view does not give a clear vision of how are you handling those objects. Try different visualizations in order to detect message calls. Take a look at this link http://www.infoq.com/articles/java-profiling-with-open-source – fGo Jun 17 '13 at 14:03

1 Answers1

3

No, you cannot replace an object on heap. What you can do is to remove all references to that object and delete it from the list, and Java's Garbage Collector will destroy the object.

See this link on Garbage Collection: http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx

darijan
  • 9,725
  • 25
  • 38
  • Thank you @darijan, But I don't want to call GC manually. Because, I heard it is not the right way to call GC explicitly by using `Sysytem.gc()`. – vissu Jun 17 '13 at 14:05
  • 1
    You don't have to call System.gc(). Java does that automatically. – darijan Jun 17 '13 at 14:07
  • Yes darijan, JVM will do that. But as more users are accessing my site I am running out of memory frequently. – vissu Jun 17 '13 at 14:09
  • 2
    Then you have another problem. If it's not a memory leak, you need more memory for your server. But investigate if the GC really does pick up the objects. Google how to test for memory leaks. ALL references to ALL objects from the list must be removed, and ALL elements from that list must be removed. They MUST NOT be references ANYWHERE. – darijan Jun 17 '13 at 14:13
  • Thank you very much @darijan, I will check it again to find memory leaks. – vissu Jun 17 '13 at 14:16