• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reclaiming memory in java without using garbage collector........  RSS feed

 
nagul samy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

is there any way to manage memory in java without using GC......???
 
Deepak Rao
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nagul

No it is not possible to manage memory on your own in Java, unlike in C,C++ using allocs and free().
If you are not happy with some specific behaviour of GC , like frequency,pauses etc you can opt for different algorithms/parameters.

Deepak
 
nagul samy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepak.......,,,
Can you please elaborate about GC's behaviour and algorithms/parameters..........
 
renu sogi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can use finalize() method in our program......
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
renu sogi wrote:We can use finalize() method in our program......

Which will not let you manually manage memory. There are very few reasons why you would ever override the finalize() method. In fact, in my 15 years of programming in Java, I've never had the need to implement my own finalize() method.

See Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding the finalize method does not allow you to manage GC. It simply provides a place for some 'tidy up' code which will be called when GC runs.
 
nagul samy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again.......
Because i was asked in an interview long back...... that is except GC what are the other ways to manage memory in java....???
And what is the difference between System.gc() and finalize() methods.........
 
Dattatraya Tembare
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagul samy wrote:thanks again.......
Because i was asked in an interview long back...... that is except GC what are the other ways to manage memory in java....???
And what is the difference between System.gc() and finalize() methods.........


See, the purpose of System.gc() and finalize() methods is different.
System.gc() or Runtime.gc - We can request GC to release memory, can't force.
finalize() - will called before garbage collection, means before object get garbage collected.
We can do one more thing we can make objects available for garbage collection.
Ex. A a = new A();
.........
.........
a =null;
Now the object of A is available for garbage collection.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dattatraya Tembare wrote:We can do one more thing we can make objects available for garbage collection.
Ex. A a = new A();
.........
.........
a =null;
Now the object of A is available for garbage collection.

Assuming of course that in the code represented by
.........
.........
no other references to that object are created.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it was an interview question, the correct thing to say is how much more reliable automatic memory management is than malloc() and free().
 
nagul samy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot........ so the answer for that question is, there is only one way to manage memory in java that is GC..........
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer is, it is much more reliable to forget about it. Trying to do your own memory management is like trying to drive a car from here to Newcastle (slightly > 40 miles) by pushing it, rather than turning the engine on.

Let the machine do the work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!