• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory management in Java application

 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we write a Java application what are the Technics that we can use to manage the memory usage

thank you
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need not worry about Memory management- JVM will take care on your behalf
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or may be you can try to limit/monitor the object creation. But I have not thought much about memory management while developing application in Java
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. what about mobile devise.they have limited memory. so when we create program we have to consider about it as I think
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:You need not worry about Memory management- JVM will take care on your behalf


could you please explain me how the JVM does it.is there a way to handle it with code. because when it is come to mobile they have limited resours
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM has Garbage collectors that will execute when ever heap is short of memory. You cannot directly influence the JVM to run the garbage collectors. And you would have to carefully develop applications considering the platform.
On a lower memory devices you will not have access to all the APIs (that which are in JavaSE) and also one would have to be careful enough while developing applications for platforms with lower resources.
 
Ifteqar Ahmed
Ranch Hand
Posts: 85
Java Linux MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Keep the following points in your mind when coding an application for memory management.

Always close the resources when you think they are no longer needed
Avoid using String use StringBuffer
Be careful when using Loops in your code
Clean up heavy objects by setting their values to NULL when you think they are no longer needed
If Appropriate force for garbage collection using RunTime.gc()

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ifteqar Ahmed wrote:Always close the resources when you think they are no longer needed

That is generally a good practice.

Avoid using String use StringBuffer

While this cuts down on intermediate object creation, it doesn't help with memory issues, all the objects are automatically reclaimed by the GC. What's more, object creation has long ceased to be a drag on the JVM.

Be careful when using Loops in your code
Clean up heavy objects by setting their values to NULL when you think they are no longer needed
If Appropriate force for garbage collection using RunTime.gc()

These are of no value with respect to memory management, or downright discouraged. Don't follow these.
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can I have more instruction about this? please
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you don't hold references to objects longer than you need to. A big problem is putting objects into a collection and keeping that collection in scope when you no longer need it. Once it's out of scope, Java's garbage collection will take over and there's no need to set references to null right before they go out of scope (e.g. before a method return), which I still see people doing. However, it's up to you to intelligently control the scope of your objects.

Look for ways to stream data. Take XML parsing for example. There are parsers that force you to read the whole structure into memory and others that let you read it in bit by bit. If memory is a big issue, use the bit by bit parsers. The same goes for any kind of data processing you're doing. If you don't need the whole data structure in one place to do your processing, just read a chunk of it, do your processing, and pass that chunk on to where ever it's going before reading the next chunk.

With regard to Strings, I'm a bit confused. I vaguely remember the JVM doesn't garbage collect unused Strings the same way it does with other Objects. Maybe that's only true of String literals? In any case I do recommend using StringBuilder (not StringBuffer unless you need thread synchronization) to build up Strings from parts. Don't go nuts with it though. You don't need it to concatenate two or three strings, but when you're doing something more complicated, it's definitely a good tool.
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

suppose a collection contains are references to other objects as elements.

so when collection variable go out of scope will all objects referenced by this collection also get freed when Garbage Collector runs?

or we need to set all referenced objects to null explicitly?

Thanks...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They'll all get freed as long as there are no other references to them. It also doesn't matter if, for instance, two objects refer to each other. As long as nothing that's in scope is referring to either of them the garbage collector will realise they're isolated and can clean them up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic