freeing memory

R

Rajesh.Rapaka

Hi,

I am using internal frame in my program. How can i free the memory it
used when i close and Internal frame ??

plz help.

regards,
Rajesh Rapaka.
 
B

Boudewijn Dijkstra

Rajesh.Rapaka said:
Hi,

I am using internal frame in my program. How can i free the memory it
used when i close and Internal frame ??

Dispose and set references to null.
 
R

Rajesh.Rapaka

Hi Boudewijn,

I've done that. But no use. I've even tried gc() and freememory()
funtions. neither helped.

any other ideas??

regards
Rajesh Rapaka.
 
T

Tony Morris

Rajesh.Rapaka said:
Hi Boudewijn,

I've done that. But no use. I've even tried gc() and freememory()
funtions. neither helped.

any other ideas??

regards
Rajesh Rapaka.

Helped what?
Setting references to null does nothing more than set references to null -
don't be fooled by the ever dominant fallacies that exist out there.
Do you have a resource leak? Grab yourself a profiler and find it.

--
Tony Morris

JTiger Unit Test Framework for J2SE 1.5
http://www.jtiger.org/
Java Q&A (FAQ, Trivia)
http://qa.jtiger.org/
http://xdweb.net/~dibblego/
 
R

Rajesh.Rapaka

hi,
Helped what?
Neither of the process helped in freeing the memory. I've set the
resources to null and garbage collected it. but this wouldnt help me in
freeing the memory. how can i free the memory?

regards
Rajesh Rapaka.
 
T

Tony Morris

Rajesh.Rapaka said:
hi,

Neither of the process helped in freeing the memory. I've set the
resources to null and garbage collected it. but this wouldnt help me in
freeing the memory. how can i free the memory?

regards
Rajesh Rapaka.

You can't set a resource to null, nor can you garbage collect it, so it
seems you have a few concepts to brush up on.
Grab yourself a profiler and get cracking - usually only takes 20-30 seconds
to diagnose the issue if it's easily produced.

--
Tony Morris

JTiger Unit Test Framework for J2SE 1.5
http://www.jtiger.org/
Java Q&A (FAQ, Trivia)
http://qa.jtiger.org/
http://xdweb.net/~dibblego/
 
D

dave

Neither of the process helped in freeing the memory. I've set the
resources to null and garbage collected it. but this wouldnt help me in
freeing the memory. how can i free the memory?

You simply cannot free memory 'by hand'. If you set references to
null, it just means the the garbage-collector COULD free the memory
if it wants to.

And even if you call the garbage-collector yourself, that's just
a SUGGESTION to the VM to run the garbage-collector.

In reality, the garbage-collector will only start to free memory
if the memory on the computer is starting to run out (!).
 
?

.

hi,

Neither of the process helped in freeing the memory. I've set the
resources to null and garbage collected it. but this wouldnt help me in
freeing the memory. how can i free the memory?

You cannot explicitly free memory in Java. You can set all references to
an object to null. If nothing is referencing the object that the garbage
collector SHOULD free the memory. If you want it to free immediately then
you can call System.gc(); and it might run the garbage collector right
away. Note that the API docs clear say that calling System.gc() SUGGESTS
that the JVM expend effort towards recycling ununsed objects.

Bottom line, you cannot guarantee that the system will reclaim the memory.

If you are getting OutOfMemoryExceptions and there are objects that you
believe have no references to them that are not getting freed then maybe
you have a memory leak. Google on "java memory leak" and you should find
some articles on it.

The most common problem is that you have a reference to the object and you
just don't realize it. For a large system it will tend to be a reference
to an object that has a reference to an object that has a reference to the
object you believe should be free. In other words, it is not always that
easy to spot.
 
D

Daniel Dyer

In reality, the garbage-collector will only start to free memory
if the memory on the computer is starting to run out (!).

That's not true, it depends on the JVM implementation (the JVM spec. does
not define how a garbage collector should behave) and its configuration.
Generally the heap cannot grow beyond a pre-configured maximum (-Xmx on
Sun VMs) so it will not use all of the computer's memory (unless you have
configured the heap to be allowed to grow that big).

Sun's VMs will increase the heap size as required but never reduce it.
With this behaviour the heap size would rapidly hit the maximum permitted
if they didn't ever perform garbage collection before the limit is reached.

System.gc() doesn't guarantee garbage collection will be performed, but it
usually is performed when this call is made.


Dan.
 
T

Tim Ward

"." said:
The most common problem is that you have a reference to the object and you
just don't realize it. For a large system it will tend to be a reference
to an object that has a reference to an object that has a reference to the
object you believe should be free. In other words, it is not always that
easy to spot.

Swing-type listener lists are a good place to start looking :)
 
T

Thomas Schodt

Daniel said:
Sun's VMs will increase the heap size as required but never reduce it.

I have logs of the number returned by Runtime.totalMemory() decreasing
over time. But maybe what it reports is not the heap size?
 
M

Mark Johnson

You cannot explicitly free memory in Java. You can set all references to
an object to null. If nothing is referencing the object that the garbage
collector SHOULD free the memory. If you want it to free immediately then
you can call System.gc(); and it might run the garbage collector right
away. Note that the API docs clear say that calling System.gc() SUGGESTS
that the JVM expend effort towards recycling ununsed objects.

Bottom line, you cannot guarantee that the system will reclaim the memory.

System.gc() will of course 'suggest' to the JVM that it should reclaim
garbage to the system heap. My past experiences with a variety of
Java platforms (Windows, Solaris, HP-UX, IRIX to name just a few)
is that there is a lot of variation within JVM implementations as to
whether they will release unused heap back to the OS global
memory pool. In most cases Unix systems will NOT do so...gc'd
memory goes back in the JVM's free heap but utilities like 'top'
or 'swapinfo' will show that the process running the JVM has a
monotone-increasing pattern to its total memory use. Eventually
the process will hit either some per-process limit in swap, VM
pages, or something...or if the per-process limits are set really
high, a global resource will be exhausted. The only way out is to
exit the JVM and restart...

Interestingly enough, Windows (at least Windows XP and 2003)
JVM's I've used seem to release gc'd heap memory back to the
OS.

If you can get access to the source code for the JVM libraries,
look for calls to the POSIX (?) sbrk() routine, which releases
memory from a process back to the OS. A JVM that does so
will (eventually) reduce its system memory use.
 
R

Rajesh.Rapaka

Hi,

Thank you for all the answers gentlemen. But isn't there a way by which
I can free the used memory as soon as I close the window, without
acessing the VM. I believe java should actually be releasing the memory
it allocated for the diplaying the Internal Frame as soon as it is
disposed.

I somehow don't get the idea of expanding the heap or manipulating the
VM appealing. Because any program written is ment to be installed on
several machines which have various memory sizes and various virtual
machines. So I dont get the idea appealing. (plz correct if my attitued
is wrong).

regards,
Rajesh Rapaka.
 
D

digidigo

Are you asking on how to get Java to release the memory back to the
System? OR are you asking for Java to release the memory back to the
Java free heap ?

How do you know that you are not getting the results that you are
looking for?

-confused
Dave
 
E

Eric Sosman

Rajesh.Rapaka said:
Hi,

Thank you for all the answers gentlemen. But isn't there a way by which
I can free the used memory as soon as I close the window, without
acessing the VM. I believe java should actually be releasing the memory
it allocated for the diplaying the Internal Frame as soon as it is
disposed.

I somehow don't get the idea of expanding the heap or manipulating the
VM appealing. Because any program written is ment to be installed on
several machines which have various memory sizes and various virtual
machines. So I dont get the idea appealing. (plz correct if my attitued
is wrong).

You aren't "wrong," but the set of programs that would
benefit from returning memory to the O/S is smaller than
one might expect. Consider:

1: At some point in its execution, the program must
acquire a large amount of memory (otherwise, it can
only return a small amount, and this isn't a large
benefit).

2: Having acquired a large amount of memory, the program
must enter a phase where it needs far less (otherwise,
it could only return a small amount).

3: After returning the unneeded memory, the program must
continue to run for a long time (returning a gigabyte
one millisecond before termination doesn't help much).

4: There should not be another "mountain" on the other
side of the memory "valley" (otherwise the program
would need to request memory from the O/S a second
time, and re-requesting the memory would certainly
take longer than simply re-using memory that has
been hanging around).

So: The overall picture is that of a program that runs
in "phases," where the early phases require a lot of memory
and the later phases (which run for a long time) require very
little. One can imagine such programs, but they're fairly
unusual -- and some of the counter-examples might profitably
be broken into two or more programs anyhow.

Of course, there are exceptions. One can imagine a long-
running program that alternates between periods of heavy memory
use and lengthy periods of light use -- maybe it does very
little most of the time, but every hour on the hour it wakes
up and rummages through a twenty-terabyte database before going
dormant again. The system as a whole might benefit from being
able to reclaim this program's memory for fifty minutes out of
each hour. Also, one can take issue with point 4 above: if
you've got a gigabyte of useless memory pages sitting on the
swap device, it may well be cheaper to tell the O/S you don't
want them any more and then ask for new pages than to fault in
the old ones merely to overwrite their useless contents. Still
and all, the "obvious" benefits of returning memory to the O/S
usually turn out to be smaller than is "obviously" the case.

(By the way, the above is not merely airy theorizing.
At a PPOE I was coerced into writing a memory manager that
returned unused pages to the O/S, so our memory-hungry program
wouldn't look quite so piggy. Result: The program slowed down
noticeably, and the actual memory usage really didn't decrease
very much. I'm not making generalizations based on thin air;
my claims are based on a statistically significant sample of
size N=1 ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top