5 Replies - 6256 Views - Last Post: 17 November 2009 - 06:46 PM Rate Topic: -----

#1 tomlidong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 07

How to reduce the memory usage in java

Post icon  Posted 17 November 2009 - 02:15 PM

Hi guys,

I want to know how to reduce the memory usage in java or how to release the memory on java.

Thanks a lot
Is This A Good Question/Topic? 0
  • +

Replies To: How to reduce the memory usage in java

#2 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: How to reduce the memory usage in java

Posted 17 November 2009 - 02:25 PM

the one method that i sugesst u is to use like this for example if u complete your work with the socket class for example
Socket s =new Socket("loclahost,"1280");


then afer your work is done i mean any object whose work is complete you can use

s=null;
System.gc();



System.gc() will call garbage collector in java and it will release your memory so i say u always refrence an object to null once its work is done
so that jvm can clear the memory that is used by the object
I guess u get it
and ya finally but not last
Welcome to DIC family ....:P
Was This Post Helpful? 1
  • +
  • -

#3 tomlidong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-October 07

Re: How to reduce the memory usage in java

Posted 17 November 2009 - 02:40 PM

Thanks for your help

I have another question. I want to release memory in Java.

For example:
Assume I have the arraylist 'oneList'containig lot of objects
Can I use the following way to release memory:
Arraylist oneList = null;

Please tell me if you have the better to do that

Thank you very much
Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: How to reduce the memory usage in java

Posted 17 November 2009 - 02:47 PM

ya i dont thinkso any problem it is actually if it is like this
Arraylist al =new  Arraylist();


then you can release it as fallows
al=null;


any thing can be released via null i mean objects ya but be carefull to release them if and only if u dont need them or else you will get stuck at
an nullpointerexpection so be carefull i guess if u have some tool called profiling in netbeans it determines the java memory usage

This post has been edited by prajayshetty: 17 November 2009 - 02:54 PM

Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: How to reduce the memory usage in java

Posted 17 November 2009 - 04:16 PM

View Posttomlidong, on 17 Nov, 2009 - 03:40 PM, said:

I have another question. I want to release memory in Java.


Sorry, not possible. Java implements something called garbage collection. This means is frees unused resources when it sees you don't need them anymore. It makes memory leaks hard, but not impossible, to create. In Java's model, it also means you can't explicitly control this process; it happens when the JVM determines it should.

If you have code you believe is using more resources than is should, feel free to post it and we'll all take a look.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to reduce the memory usage in java

Posted 17 November 2009 - 06:46 PM

And dont get confused

al = null;

will relase the ArrayList but not the objects it contains (if they are referrenced somewhere else)

And despite general feeling ArrayList does not take a lot of memory it just contains pointers to the objects it contains

An arrayList of 10 Integer does not take more space than an ArrayList of 10 AnnulaReport objects withh 10000 numbers in them
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1