-4

I understand that in Java, we usually do not need to worry about deleting variables manually since garbage collection can take care of that. However, in some situations, we need to free an object before garbage collection takes effect in order to prevent memory leak. To free an object, we can simple set its reference to be null.

Now my question is, how can we manually free a primitive variable? If there is no way to manually free a primitive variable, then why, since there is a mechanism to manually free a reference variable?

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
Chao Zhang
  • 15
  • 2

2 Answers2

2

This question is a nonsense. It is not true that by setting null you delete anything. And primitive types cannot be "deleted". The memory space for local variables is allocated as long as they are in scope.

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • Sorry for the word I used. I mean "free", not "delete". – Chao Zhang Sep 11 '16 at 21:07
  • What I am curious is how to manually free a primitive variable. Since we can free a reference variable manually, why there is no way to free a primitive variable manually. – Chao Zhang Sep 12 '16 at 02:35
  • @ChaoZhang: You actually cannot free any variable manually. You can assign null to references, you can assign 0 to int, but they both are still in memory. – Al Kepp Sep 12 '16 at 08:06
  • @AIKepp: Assigning null to a reference makes the corresponding object ready to be handled by garbage collection before the object is out of scope. That is what I mean by saying "free". I don't know the correct way to name it. I would like to know if there is an analogous method to make a primitive variable ready to be handled by garbage collection before the variable is out of scope. – Chao Zhang Sep 12 '16 at 19:26
  • @AIkepp: Assigning 0 to int is not an analogy to assigning null to a referece. For the former, you actually add 0 to the stack and make the reference point to 0, that means the memory is still held by 0, and that portion of memory can not be handled by garbage collection. For the latter, the object can be handled by garbage collection and the memory ocupied by the object can be reused. – Chao Zhang Sep 12 '16 at 19:26
  • @ChaoZhang I am affraid you are wrong. The point is that both int and reference are technically just 4 bytes of memory. By assigning null to reference variable or 0 to int variable, you technically do exactly the same operation: You write 4 bytes of zeros to the memory (on a 32bit computer). – Al Kepp Sep 13 '16 at 18:58
  • @AIKepp: Set int to 0, you get a box containing 0. Setting a reference variable to null, you get an empty box. A box containing 0 or whatever is different from an empty box. The memory of int and reference is not relevant here since we are talking about an arbitrary primitive variable, not necessarily int. – Chao Zhang Sep 14 '16 at 03:30
2

You cannot set a primitive type (int, float, double, char... a few others; you get the picture) to null. If you try, you'll get a NullPointerException.

There is no concept of delete in Java. Java handles all memory management automatically via internal reference counters. This process is commonly referred to as the Garbage Collector.

Once an Object goes out of scope, it is made available for Garbage Collection and its memory may be reclaimed at any time. You do not need to worry about de-allocating the memory manually in Java. While primitives are not freed directly by the Garbage Collector (see 'are java primitives garbage collected'), you still don't need to worry about deleting them.

(The topic of Garbage Collection is obviously much deeper and more complicated than that, but I think this post is a fair 100,000-foot summary.)

Community
  • 1
  • 1
nasukkin
  • 2,460
  • 1
  • 12
  • 19
  • Actually, garbage collection cares about objects, not variables. – Al Kepp Sep 09 '16 at 21:28
  • @AlKepp I actually didn't know that! I will amend my answer. – nasukkin Sep 09 '16 at 21:29
  • Objects are a runtime entity and scope is a compile time concept. Objects aren't collected at the end of their scope because scope doesn't apply to objects. Scope applies to names used in source code. Objects are collected when the runtime determines that no possible continuing execution of the program can access the object. – Sotirios Delimanolis Sep 09 '16 at 22:34
  • @SotiriosDelimanolis No it's not. Try it for yourself: http://pastebin.com/raw/NTMQ7um0 – nasukkin Sep 09 '16 at 22:46
  • Thanks for answering. I did say that I wanted to set a primitive variable to null. What I am curious is that, in addition to the garbage collection mechanism, there is a mechanism to manually free a reference variable, like an object, by setting its reference to free, but why there is no mechanism to free a primitive variable manually. – Chao Zhang Sep 12 '16 at 02:29
  • @ChaoZhang All primitives are defined in one of two ways. Either they are part of a stack frame OR they are defined as an Object field. When it's a stack frame, the memory becomes available once the frame goes out of scope (e.g., your method has finished executing). When the primative is part of an Object, the garbage collection of the Object itself causes the memory to be freed. – nasukkin Sep 12 '16 at 17:22
  • @nasukkin: Thanks for anwsering. I know what you have said. I would like to know a way to manually make the memory available before the frame goes out of scope. This could be done for reference variables by setting its reference to null. I was wondering if there is an analogous method for primitive variables. And if not, why. – Chao Zhang Sep 12 '16 at 19:11
  • @ChaoZhang I'm afraid that isn't possible to the best of my knowledge. The only time that stack allocations are freed is when the method returns. – nasukkin Sep 12 '16 at 21:45