-6

I haven't found or discovered a clear answer of this yet. I am not asking about a specific language but am talking about all OOP languages in general.

If I have an object for example:

Object obj = new Instance();

Lets say I no longer need this object. Of course I could simply do as follows:

obj = null;

However, correct me if I am wrong, this simply means the obj variable is no longer assigned as "new Instance();" but does not mean that "new Instance();" no longer exists. So my question is: How can I make sure the instance object is COMPLETELY removed from the system and no longer exists. How can I be completely sure the instance no longer exists?

I program in Objective-C, Java, and C#, so an answer relative to those languages would be great as I am aware garbage cleaners work differently in different languages and in some languages don't even exist.

I know you cannot completely clear an instance in some languages however what would be the best way to free up memory with the object?

Thanks so much!

jer_francis
  • 153
  • 1
  • 11
  • 6
    For Java and C# at least, you simply can't. – Jon Skeet Jun 19 '14 at 14:51
  • 2
    I don't know of a way to *force* Java to delete an object. You can call System.gc() after nullifying all of the references, but that's a *suggestion* to the JVM to perform garbage collection, and there's no guarantee that GC will run at that point, or that it will reclaim the memory from any particular object. – sumitsu Jun 19 '14 at 14:53
  • @Sumitsu I have been told System.gc(); can be very dangerous to use and is not recommended. Do you think its better to attempt to free up more memory, or to play it safe and stay away from it? – jer_francis Jun 19 '14 at 14:56
  • In objective-c it depends if you are using ARC or not. If you are, then removing all strong references to an object will immediately release it. – CrimsonChris Jun 19 '14 at 15:00
  • @jer_francis in Java and C# you should not worry about freeing memory consumption, that's the Garbage Collector's job, not yours. You only have to worry to not generating memory leaks. Additionally, in Java you can call `System.gc()` and the JVM will decide if it should fire garbage collection process, but it is not dangerous (I would want to check where you read that). I cannot speak for Objective C since I don't work with it. – Luiggi Mendoza Jun 19 '14 at 15:10
  • 2
    This should really be separated into a question for each language. I wouldn't accept any answer that didn't cover all three languages. – CrimsonChris Jun 19 '14 at 15:15
  • @jer_francis: I'm not an expert on this, so I'll refer you to what appears to be a good StackOverflow discussion on the topic: http://stackoverflow.com/questions/2414105/why-is-it-a-bad-practice-to-call-system-gc. The gist of it seems to be: (1) The JVM isn't obligated to do anything, and (2) If the JVM *does* oblige you and runs GC, then you've likely made your code less efficient by overriding the JVM's normal garbage-collection cycle. With those two points in mind, any code which *relies* on a call to System.gc() probably has some fundamental issues. – sumitsu Jun 19 '14 at 15:37

3 Answers3

0

In java and C#, your garbage collector takes care of that and there is nothing you can or should do to reclaim that memory.

radumanolescu
  • 4,059
  • 2
  • 31
  • 44
-1

As others have said, the removal of the object from memory itself is out of your control with Java. Your only requirement is to unassign all references to this object so that it becomes eligible for garbage collection, but when GC actually runs is neither determinable nor reliably consistent.

There is a System.gc() call which you can read into, but it should probably just be avoided entirely.

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
  • Answer is incomplete. Where is the comparison with objective-c? – CrimsonChris Jun 19 '14 at 15:10
  • 1
    -1: Sorry Kon, but I don't know how can this be the accepted answer since you only talk about Java and leave C# and Objective-C out of the answer. – Luiggi Mendoza Jun 19 '14 at 15:11
  • @LuiggiMendoza By my understanding, a comparison would be beneficial to the OP, but not required. I'm not going to disagree with your downvote, but the information I provided above is helpful and accurate, just omits details on which I have no knowledge. – Kon Jun 19 '14 at 16:04
  • I have a good understanding about GC in Java, but that doesn't mean I should post it as answer because it doesn't address OP's question. In fact, the question is too broad to be answered. – Luiggi Mendoza Jun 19 '14 at 16:05
-1

If you're programming with C# or Java they can do it for you. I develop with C# so I can talk about it, if you want to force Garbage Collector to delete unused objects for you you can call System.GC.Collect() method can help you.

Hasan Akgün
  • 467
  • 1
  • 4
  • 15