File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Confusion on Garbage Collection and Memory Consumption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Confusion on Garbage Collection and Memory Consumption" Watch "Confusion on Garbage Collection and Memory Consumption" New topic
Author

Confusion on Garbage Collection and Memory Consumption

Andres Delrotti
Ranch Hand

Joined: Aug 11, 2005
Posts: 139
Hi everyone,

First of all pardon my english if you cant understand it that well. Ill try my best.

We are currently load testing an application deployed on Websphere 5 running on Win2k3 OS. When we view the javaw.exe memory consumption in the task manager, it says it consumes 630MB. When we examine the garbage collection data, majority of the memory usage before garbage collection was 512MB then goes down to 130MB after garbage collection. If that is the case, why does javaw.exe in the task manager show the memory consumption playing around 630-645MB? Should it be going down to 130MB and up again to 512MB if that is what is shown in the garbage collection data?

2nd question is, after the load testing, we are expecting the memory to go down and stabilize to a lower value. How come it stays at 630MB even after our load test? shouldn't it be lowered because of garbage collection? Quite odd since based on garbage collection data, we can still see memory being freed up although no longer that often. Does the windows task manager really show it that way or does that say there is a memory leak in our app?

Our websphere settings:
Initial heap size: 512MB
Max Heap size: 1.5GB
Server machine: 3GB RAM

Thanks. I would appreciate any help or advise.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

The behavior that you observed is completely normal.

First, when Java obtains memory from the OS, it never releases it back, even if the objects in the heap are released. It just holds on to the memory and recycles it for the next object being allocated on the heap. This is done because obtaining memory from the OS is expensive. So, Java holds on to the memory and reuses it

Second, GC is a very lazy operation. GC will not collect objects until it absolutely has to, or an internal timer triggers it. This is because GC is costly, and GC benifits from economy of scale. The more objects to collect, the faster it is to collect individual objects. So, the expected behavior on an application that is mostly idle is that heap usage grows till an internal timer triggers and cleans the heap. If the application is active and using lot of memory, GC will be triggered when heap usage hits max. If you watch memory usage on a tool like JConsole, you will see a graph that looks like a saw. This is completely normal.

What you want to look for is the bottom of the saw stays straight (or atleast goes down once processing is complete). That's your "real" memory usage
Andres Delrotti
Ranch Hand

Joined: Aug 11, 2005
Posts: 139
Jayesh A Lalwani wrote:The behavior that you observed is completely normal.

First, when Java obtains memory from the OS, it never releases it back, even if the objects in the heap are released. It just holds on to the memory and recycles it for the next object being allocated on the heap. This is done because obtaining memory from the OS is expensive. So, Java holds on to the memory and reuses it

Second, GC is a very lazy operation. GC will not collect objects until it absolutely has to, or an internal timer triggers it. This is because GC is costly, and GC benifits from economy of scale. The more objects to collect, the faster it is to collect individual objects. So, the expected behavior on an application that is mostly idle is that heap usage grows till an internal timer triggers and cleans the heap. If the application is active and using lot of memory, GC will be triggered when heap usage hits max. If you watch memory usage on a tool like JConsole, you will see a graph that looks like a saw. This is completely normal.

What you want to look for is the bottom of the saw stays straight (or atleast goes down once processing is complete). That's your "real" memory usage


Thanks Jayesh. Learned something new there.

So....when my applications reached a memory consumption equal or slightly greater than whatever was set in the initial heap size (-Xms) of my JVM, thats the only time GC would run? Is that how it really works? If yes then whats the point of setting the maximum heap size (-Xmx)?

and when we take a look at the windows task manager....whatever memory size we see the javaw.exe consuming is the memory allocated to the JVM? and not the total memory every java application consumes in real time? Am I correct?

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

Andres Delrotti wrote:

Thanks Jayesh. Learned something new there.

So....when my applications reached a memory consumption equal or slightly greater than whatever was set in the initial heap size (-Xms) of my JVM, thats the only time GC would run? Is that how it really works? If yes then whats the point of setting the maximum heap size (-Xmx)?

-Xms controls how much memory the Java process will obtain from the OS as soon as it starts(no matter what the avtual heap usage). -Xmx controls the maximum memory Java will obtain from OS

When the app is idle, GC will run periodically, no matter what the usage. WHen the app is busy, GC will run when heap usage hits -Xmx



and when we take a look at the windows task manager....whatever memory size we see the javaw.exe consuming is the memory allocated to the JVM? and not the total memory every java application consumes in real time? Am I correct?



Yes. For checking the size of the heap, you have to use tools that come with JDK.. like JConsole, or take a heap dump. You cannot rely on the OS to report the size of the heap correctly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion on Garbage Collection and Memory Consumption