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.
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.
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
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