-2

How can I reclaim the memory used by an object and all of its data structures into my application memory without System.gc() which only suggests and not necessarily perform memory check. This also includes methods like finalize.

In other words, assume I have a HashMap and I want all the data it stores to be deleted from the memory. I normally do clean this type of collections with clean methods or attach it to null but that doesn't seem to return the memory space to the application?

Edits:

Let us say the following:

    TIntObjectHashMap<byte[]> map=new TIntObjectHashMap<byte[]>();
    //fill the map with 20GB
    map.clear();
   //Or map=null;

Shouldn't the 20GB goes back free? That is the question.

DotNet
  • 697
  • 2
  • 7
  • 23
  • An object is available for garbage collection when there are no more references to it. – Brian Roach Mar 01 '13 at 21:11
  • Thanks to whoever pointed the other question. – DotNet Mar 01 '13 at 21:17
  • @BrianRoach in fact, is eligible when any alive thread can access the object. If you have A "pointing" B and B "pointing" A, and no thread can access either A or B, both objects are eligible, however they have mutual references. – psabbate Mar 01 '13 at 21:20
  • @psabbate - Yes, I'm quite aware of how the garbage collector works. Circular references cut off from the GC root is a *bit* beyond the scope of this (dup) question and I really didn't see the need to mention it. – Brian Roach Mar 01 '13 at 21:25

2 Answers2

9

Basically, you can't. There's nothing you can do to absolutely force garbage collection, and you certainly can't manually delete an object.

Learn to love and trust the garbage collector.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 5
    +1 for `Learn to love and trust the garbage collector.` :) – PermGenError Mar 01 '13 at 21:12
  • Jon, thanks but that seems to me a real issue with large-scale projects. I need to handle lots of data but I don't want them to hang around for long?! – DotNet Mar 01 '13 at 21:19
  • 3
    Do you have any *evidence* that it will be a problem? Java is widely used in large projects. There are ways of tuning garbage collection, but even they're often not needed - the defaults are pretty reasonable. – Jon Skeet Mar 01 '13 at 21:25
  • @DotNet: The garbage collector is almost magically smart. Trust that it won't let unused objects pile up. – Louis Wasserman Mar 01 '13 at 21:30
  • @LouisWasserman thanks. I know and I trust it but I think *clear* in collections should clear the references and let the GC whenever it feels needed collect them. – DotNet Mar 01 '13 at 21:38
  • @DotNet: Yes, and it does. Any collection where `clear()` *didn't* clear the references it was holding onto would be broken. However, I rarely find myself calling `clear()` explicitly anyway - I just let the collection object itself get garbage collected, usually. – Jon Skeet Mar 01 '13 at 22:31
2

You can't force garbage collection, the only thing you can do is to call the System.gc() method, but it's just a hint to the garbage collector, it will not force it to do a collection

BackSlash
  • 21,927
  • 22
  • 96
  • 136