Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Memory Handling -de allocate memory in Java

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Memory Handling -de allocate memory in Java

    Quote Originally Posted by 19world View Post
    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.

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Memory Handling -de allocate memory in Java

    I am not priviledged to access this server. I run as remoted :S via deployment

Similar Threads

  1. Out of Memory Error
    By wasaki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2010, 03:37 PM
  2. Out of memory work around for a java application (please help!)
    By javameanslife in forum Java Theory & Questions
    Replies: 5
    Last Post: January 22nd, 2010, 04:27 AM
  3. free memory of bufferedimage
    By mr_empty in forum Java SE APIs
    Replies: 2
    Last Post: January 19th, 2010, 06:14 AM
  4. Out of memory - Java heap space
    By fraxx in forum Java Theory & Questions
    Replies: 4
    Last Post: November 24th, 2009, 05:26 AM
  5. Java memory management
    By trueacumen in forum Java Theory & Questions
    Replies: 5
    Last Post: August 12th, 2009, 02:40 AM