• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory management

 
Ashan Ranasinghe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks
Ashan
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Layne
 
Gadi Reddy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you just see "Garbage collection". this feature is availble in java to free the memory.that's why java is so popular.
Regards,
Raghu
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashan Ranasinghe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Ashan
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic