wood burning stoves*
The moose likes Performance and the fly likes JVM is taking memory and not releasing it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "JVM is taking memory and not releasing it" Watch "JVM is taking memory and not releasing it" New topic
Author

JVM is taking memory and not releasing it

omar bili
Ranch Hand

Joined: Aug 13, 2004
Posts: 177
Hi,
my JVM is taking memory and not releasing it to the system even after destroying the objects that i have created!
i have called the Thread.yield() ; Runtime.getRuntime().gc() ;
and nothing seems to be working!
Thanks Experts
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

That's how Sun's JVMs are implemented. Memory requested from the OS becomes part of one contiguous block in the JVM process called the "Java heap." Objects are allocated in that heap. When objects are freed, they are released to that heap. The Java heap can grow, but it never shrinks -- although sometimes it contains free space. There are a few methods in the Runtime class that let you find out how big the Java heap is, and how much free memory it contains.


[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Java does not trade memory back and forth with the operating system - it tends to request a block and then keep it in anticipation of a continued need. There are various command line parameters you can use to control some aspects of memory management.
Look at the java.lang.Runtime class for methods that let you see how much memory has been used or freed by gc.
Bill
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
If I agree to both of you, could you please suggest the use of parameters "MinHeapFreeRatio".

Currently we are facing problem that Windows memory keeps on growing though JVM heap has enough memory(I am using OptimizeIt for profiling).

JBoss 3.2,JDK 1.4.2 is runnning on Windows 2003.

Enviorenment is
RAM : 2 GB
Paging Size for All Drives:
Intial : 3070
Maximum : 6140

CPU: Intel Xeon 3.00 GHz
OS : Windows Server 2003, Enterprise Edition, Service Pack 1

What I observe that Windows memory keeps on growing and then JVM crashes once it reaches to 2G.

It seems that JVM is not utilizing freed(after GC) memory.

I googled net for this and found that most people are suggesting to use Max/MinHeapFreeRatio params.
But somehow it seems not to working for me

Currently I am reading more material on JVM and its tuning.

Any article/source or any suggestion or help would be appreciated.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
What I observe that Windows memory keeps on growing and then JVM crashes once it reaches to 2G.

It seems that JVM is not utilizing freed(after GC) memory.


I think you may be drawing the wrong conclusion. Why are you so sure that memory has in fact been freed? What do you get from calls to Runtime.freeMemory()?
Bill
Anurag Jain
Greenhorn

Joined: Jul 23, 2002
Posts: 8
The Java heap can grow, but it never shrinks -- although sometimes it contains free space.


Does this mean that JVM can not release memory even after application execution is complete?
I am running my memory intensive application and when I check the memory available after my application completes, even then memory is not freed.


--<br />Anurag
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
when I check the memory available after my application completes, even then memory is not freed.

What do you mean by the application completing? If the program JVM exits normally you should get back all the memory.

On the question of JVM releasing memory - according to the Windows XP task manager, I can watch the memory being used by Tomcat and the "available memory" go up and down quite a bit - Tomcat 5.5.9 and Java 1.5.
Bill
Anurag Jain
Greenhorn

Joined: Jul 23, 2002
Posts: 8
What do you mean by the application completing? If the program JVM exits normally you should get back all the memory.


I am not running a web application, I am running a simple core java application and even after it exits normally it does not release the memory.
I am using jdk1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If it exits, normally or abnormally, then the process is destroyed, and so there would no longer be an entry in the "Task Manager" display for that process, and so no place to even look for the memory usage. If you see "java.exe" in the Task Manager, then there's a still-running Java program.

Perhaps you've written a GUI program that doesn't specifically exit when all the windows are closed? This is a fairly easy newbie mistake to make.
Harald Kirsch
Ranch Hand

Joined: Oct 14, 2005
Posts: 37
Originally posted by R K Singh:
If I agree to both of you, could you please suggest the use of parameters "MinHeapFreeRatio".

The JVM does return memory to the operating system. I use -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=10 frequently for a process that needs 1GB at startup until it is settled, but then runs fine with 250MB forever. See http://www.ebi.ac.uk/~kirsch/monq-doc/monq/stuff/ConvinceGC.html for how I did it. This is on Linux, however.
[ October 14, 2005: Message edited by: Harald Kirsch ]

Harald.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by William Brogden:
Why are you so sure that memory has in fact been freed? What do you get from calls to Runtime.freeMemory()?
Bill


Hi William,
Actually when I run GC from OptimizeIt, I see significant drop in heap but on Task Manager, I fail to see any drop.

My guess is that is is because of OptimizeIt tool.


Originally posted by Harald Kirsch:
This is on Linux, however.


I am using Windows Server 2003.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Perhaps you should upgrade to Java 1.5 - thats what I am using and I do observe the Task Manager report of memory used going down.
Bill
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by William Brogden:
Perhaps you should upgrade to Java 1.5 - thats what I am using and I do observe the Task Manager report of memory used going down.
Bill


Thanks William, I wish that this thing has been taken care in Java1.5

Currently I cant migrate as production env is JDK1.4.2 with JBoss 3.x
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JVM is taking memory and not releasing it
 
Similar Threads
Can JVM release memory back to OS?
Memory Management in Java
Migration from Websphere 4 to 5.1.1.8 fop issue
Releasing memory to OS
Release heap memory