Memory Handling -de allocate memory in Java
Hi All,
I would like to ask a question regarding Java programming and memory handling. I want to ask if there is any way for the creator of a program to allocate and de allocate memory at any time he wills. Especesially deallocation matters. I know about java gc that is called automatically when we do not use any variable specified but what happens if in a long method we declare a string array and we use this many times (example this method is called many times in one application). When the first call is executed and the result is stored to this array is OK. After wards and in the meantime before reuse it how can I de allocate the captured memory? Another question regarding memory is the difference between the following declarations :
1) String []temp=new String[200];
2)String []temp=new String[200]; for (int i=0; i<temp.length; i++){temp[i]=null;} //This called any time the method is called.
Does null initialization helps in memory de allocation ?
This is a theory question moral less.
Thank you all for your time.
Regards
19world
Re: Memory Handling -de allocate memory in Java
Hello, there is no specific way to allocate and free up memory in Java programmatically but there are a few ways to help the JVM out in its memory management.
1. Null variables in your code once you've used them, this might help.
2. Give the JVM more memory to start with using the -Xmx<X>m feature, for instance -Xmx128m will give the JVM 128Mb of heap and -Xmx1g will give it 1Gb of memory.
Once a method finishes all the variables in that method will be eligible for garbage collection so nulling variables inside a method might be more of a hassle then just letting it run to its finish. However if you think the method call will take a long time and a lot of memory will be used, then by all means, start nulling.
Hope this helps, the whole Java and memory management is a science in its own and there is loads of different whitepapers out there on the issue.
See Java HotSpot VM Options for the options you can use when running the JVM.
See Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine for garbage collection tuning.
// Json
Re: Memory Handling -de allocate memory in Java
Hi,
Thank you for your reply. It is helpful also now I ll read the Tuning GC link you provided.
I thought null will help and such a verification will help. Assume that we have a program to run on a server behind an automated task, creating many instances (k) for many orders (p) and every instance needs minimum N seconds to finish.
So the time is G.equal to k*p*N if we have instances in parallel(absolute parallel is not true, we always have some ms delay). So if we use the class without nulling I believe we capture more memory. I may be wrong, is the first time I met this issue.
However , I do not know how to see the memory that is captured or free at any time. Windows task manager does not help a lot ..
Any further information will be helpful..
Thanks
19world
Re: Memory Handling -de allocate memory in Java
Quote:
Originally Posted by
19world
However , I do not know how to see the memory that is captured or free at any time. Windows task manager does not help a lot ..
If you're running on a server, the server may have monitoring tools that will show you the memory use by processes. Otherwise, you'll need a memory profiler. There are free/open source profilers out there if you search for them.
Re: Memory Handling -de allocate memory in Java
I am not priviledged to access this server. I run as remoted :S via deployment