aspose file tools
The moose likes Beginning Java and the fly likes Memory management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory management" Watch "Memory management" New topic

Memory management

Ashan Ranasinghe

Joined: Oct 06, 2004
Posts: 8
Hi All,
This is a question that has botthered me for some time.

ok say i write an application in java for example a cafe software. now this software will be running the entire time and i've noticed that the longer an application is running the more memory it takes and the machine slowly starts to slow down. i noticed this especially when using limewire. is there any way i can automatically clear memory or reduce the amount of memory that java uses. or is there some way of coding so that the application automatically restarts itself without loosing any data after flushing memory.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It sounds like you need to read up on garbage collection. This is the mechanism that the JVM uses to free memory back to the system when your program no longer needs it. Unfortunately, there is no way to force the JVM to "automatically clear memory." However, if you assign null to all the references to an object, it becomes available for garbage collection. This means that when the JVM decides to run the garbage collector the memory used by that object is freed. Notice that you have to make sure that ALL references to an object are null or out of scope. This can be a little tricky when you are passing object references to methods that keep a copy of the reference. For example, in GUI program, you often create event listeners that are registered with a GUI component. If a particular event is of no concern any longer, then you should probably remove the event listener from the component so that it is available for garbage collections.

I'm sure there are Internet sources that explain this much better than I can. Garbage collection has also been discussed on several forums here at the Ranch, so you might want to use the search tool to find previous discussions on the topic.

I hope this helps.


Java API Documentation
The Java Tutorial
Gadi Reddy

Joined: Jan 10, 2006
Posts: 21
you just see "Garbage collection". this feature is availble in java to free the memory.that's why java is so popular.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14492

In Java, objects that aren't referenced anymore are cleaned up automatically by the garbage collector. You don't have to free allocated memory explicitly as in C, C++ and other programming languages.

However, that does not mean that your Java program cannot have memory or resource leaks. Have a look at the following: Handling memory leaks in Java programs

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ashan Ranasinghe

Joined: Oct 06, 2004
Posts: 8
thanks guys for the feedback.

is there anyway that i can optimize programms that i download that are written in java (example limewire) if i keep it running for more than an hour and a half it just about saps whatever little memory i have
i have 256 ram and a 128mb nvidia card

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you watch a Java program from the operating system perspective you'll proabably see it grab more memory now and then but never release any. There is a command line parameter that tells the JVM the maximum amount it can grab. The JVM manages objects within that amount. When it garbage collects an object it makes some free space within the memory it owns, but it doesn't release the top end of the memory back to the operating system. (Actually some JVMs might!) So if it takes a program a long time to get up to using the maximum it might look like a memory leak from the Windows task manager or similar tools, even if it's not.

If you compile your programs with JDK5 look at the JConsole tool that comes in the JDK. It will show you memory use within the big chunk the OS gave it going up and down with garbage collection.

Try entering "java -X" to display help on the command line options that limit memory among other things.
[ January 24, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Memory management