• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object deletion vs Member variable deletion in Java.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To delete an Object in java means freeing that memory space occupied by that object previously.



Can we do the same to its member variables. Suppose I don't need 'int b' in the above program. 'b' is of no use so can we free that space occupied by 'b' only not 'a' and 'c'. Is it possible or is it related to the phenomenon of encapsulation ?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rasul Patrick wrote:To delete an Object in java means freeing that memory space occupied by that object previously.



I am not sure if delete is the word I would use. De-reference sounds better. When you deference the member variable, the object may still be on the heap only to be garbage collected by the JVM's internal mechanism later on.

Rasul Patrick wrote:

Can we do the same to its member variables. Suppose I don't need 'int b' in the above program. 'b' is of no use so can we free that space occupied by 'b' only not 'a' and 'c'. Is it possible or is it related to the phenomenon of encapsulation ?



Read this article. See if it answers your question about freeing member variable space in Java. That is an area which we as Java programmers need not worry about. If you really want to understand how the GC works, you will have to patiently study this article.

EDIT: Experts please pitch in on this.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can ever free up the memory space.

You can null out the reference variable, which (assuming ALL references are nulled or othewise unreachable) makes it ELIGIBLE for GC, but you can't be sure it ever will be.

And primitives aren't references or objects, so they can't be nulled.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:

Rasul Patrick wrote:To delete an Object in java means freeing that memory space occupied by that object previously.



I am not sure if delete is the word I would use. De-reference sounds better. When you deference the member variable, the object may still be on the heap only to be garbage collected by the JVM's internal mechanism later on.



I don't think "dereference" is the correct word here either. Dereferencing a variable means to use a reference variable.

For example, if you have a Basic object, held in a "bas" reference variable, you can do this .... "bas.a" .... which will dereference the "bas" variable to get the int primitive held in the "a" field.

Henry
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:I am not sure if delete is the word I would use. De-reference sounds better.


No, dereference already means something else entirely, in the computing world. What you describe is setting a reference to null, informally known as nulling it out.

I agree with all of Fred's comments above.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rasul Patrick wrote:To delete an Object in java means freeing that memory space occupied by that object previously.


You cannot explicitly delete an Object in Java.

An object will eventually be destroyed by the garbage collector after there are no references to the object anymore from any live threads. You have no control over when exactly the garbage collector will destroy it.

Rasul Patrick wrote:Can we do the same to its member variables. Suppose I don't need 'int b' in the above program. 'b' is of no use so can we free that space occupied by 'b' only not 'a' and 'c'. Is it possible or is it related to the phenomenon of encapsulation ?


No, that is not possible. In your example, member variable 'b' is part of the object. You can't delete part of an object.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:
No, that is not possible. In your example, member variable 'b' is part of the object. You can't delete part of an object.



However, if b was a reference variable rather than a primitive, and if there are no other reachable references to the object that b points to, and if you set b to null, then the object that b pointed to becomes eligible for GC.

In other words, it doesn't matter how an object is referenced before becoming unreachable--member variable, local variable, or just as the result of an expression--all that matters is that a reachable object is not eligible for GC and an unreachable one is eligible.

Also note that we almost never explicitly set individual member variables to null in order to "help" the GC. We do sometimes remove objects from collections, or set an array reference to null, when the object is no longer needed. In general though, you don't need to, and should not try to, help the GC by setting references to null.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Mansukhdeep Thind wrote:I am not sure if delete is the word I would use. De-reference sounds better.


No, dereference already means something else entirely, in the computing world. What you describe is setting a reference to null, informally known as nulling it out.

I agree with all of Fred's comments above.



Thank you for correcting me guys.
reply
    Bookmark Topic Watch Topic
  • New Topic