LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2008, 02:07 PM   #1
silverlander
LQ Newbie
 
Registered: Nov 2008
Posts: 1

Rep: Reputation: 0
can a dangling pointer exist in java???


I know that you're about to answer NO, but this question tries to challenge common sense. Please read.

I made a simple test class with a constructor that throws an exception. I think that an exception cancels the creation of an object when thrown by a constructor, am I right? Well, in this case the constructor saved a pointer to this in a static property before the throw statement was reached. So after the exception is caught the guilty object is still accessible through the static property.

Code:
public class DanglingReferenceTest {
	public static void main(String[] args) throws Exception {
		DanglingReferenceTest a = null;
		try {
			a = new DanglingReferenceTest();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		System.out.println("Initialized? " + DanglingReferenceTest.myInstance.initialized);
	}

	public static DanglingReferenceTest myInstance = null;

	public DanglingReferenceTest() {
		myInstance = this;
		initialized = true;
		throw new RuntimeException("Debugging exception");
	}
	public boolean initialized = false;
}
By running this test program you can see that the initialized property is correctly read from the instance even if the constructor failed with an exception.

Question 1: Has the object actually been created?
Question 2: Is the object subject to garbage collection?
Question 3: Is this actually a dangling pointer?
 
Old 11-14-2008, 02:55 PM   #2
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
The reason for what looks like a dangling pointer there is the usage of static. Static forces a storage of values between calls to the functions, in this case the constructor.

If the garbage collector has not run since the exception the static storage will not be destroyed. This is one of the main issues with Java, you have no control over when the resolved memory is destroyed and returned to the system.
 
Old 11-14-2008, 03:01 PM   #3
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 131Reputation: 131
1. No, your a object still does not exist. There is a convenient way of finding out: put a System.out.println(a==null) as the last statement of your main block; you will see that it returns true. What has been initialized, though, is the static DanglingReferenceTest member.
2. What is null does not need to be collected; there simply is not anything to collect. Null means it will get collected automatically, at some time. The static DanglingReference Object is different, though, but will be collected too as soon as it goes out of scope (in fact, it is more correct to say that it will become fit for collection as there is no predicting when the JVM will get round to running gc() - you could actually force it to by calling System.gc() but this is rarely good practice.
3. Java does not have a pointer concept. What you have instead is a reference which always refers to something, even though it is just null.

On the concept of statics, they work like this:
when the jvm finds the first reference to a class, it look for the corresponding class file and loads it. As a rule, this always and automatically causes static members of that class to be initialized (in this case, to null).
When an object of that class is made, the non-static data members are initialized to their default value (0,false, null, ...). Then its constructor is called. As the first statement executes, the static DanglingReferenceTest myInstance gets its proper value (this, which here means the current object in its current state), followed by the re-initialization of its "initialized" member. When the exception is thrown, the static object has already been constructed but the non-static in the main method has not. Thus you have two cases: a static one that is initialized (but maybe incompletely) and a non-static one that will be available only when and if the constructor exits without an exception.
I would strongly recommend to you get into the habit of avoiding this references inside constructors. They are one of the main causes of some extremely vicious bugs - and as your code grows in complexity, involving composition and inheritance, these things can get really out of hand.

Last edited by jay73; 11-14-2008 at 03:24 PM.
 
Old 11-14-2008, 03:13 PM   #4
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by jay73 View Post
1. No, your a object still does not exist. There is a convenient way of finding out: put a System.out.println(a==null) as the last statement of your main block; you will see that it returns true.

This is your only correct comment.

2. What is null does not need to be collected; there simply is not anything to collect.

As to this. From the java manual.
In Java(tm), "null" is not a keyword, but a special literal of the null type. It can be cast to any reference type, but not to any primitive type such as int or boolean. The null literal doesn't necessarily have value zero. And it is impossible to cast to the null type or declare a variable of this type.

Null is a type and takes 1bit of memory if you trace the java vm there for it is subject the garbage collector because it does use memory.

3. Java does not have a pointer concept.
there is a pointor concept, but you do not have access to it from code. It is VM controlled using the autopointer style system developed for C++. When you create and work with objects you create pointors to memory just like any other language, you just don't have direct control to the pointer.
 
Old 11-14-2008, 03:32 PM   #5
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 131Reputation: 131
2. I'll rephrase then: null objects do not require any special action to become gc ready as they already are and they will be collected either during runtime or when the JVM exits. Non-null objects need to be set to null explicitly before they can qualify for collection.

3. That makes sense but, as you say, these pointers cannot be controlled by the programmer so why mention them at all? I think this only adds fuel to the java pointer debate. I have seen more than one C++ developer seriously disappointed "because they told me java has pointers too".

Last edited by jay73; 11-14-2008 at 03:37 PM.
 
Old 11-14-2008, 03:38 PM   #6
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by jay73 View Post
2. I'll rephrase then: null objects do not require any special action to become gc ready as they already are and they will be collected either during runtime or when the JVM exits. Non-null objects need to be set to null explicitly before they can qualify for collection.
They will also qualify for gc when they go out of scope as in the example in the OP's post. It is a static constructor that throws and exception, there for the values will sit at null even though the object was not created. There for the hanging static vars will be waiting for the gc to run or the vm to end because they are static so always in scope.
 
Old 11-14-2008, 03:40 PM   #7
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by jay73 View Post
3. That makes sense but, as you say, these pointers cannot be controlled by the programmer so why mention them at all? I think this only adds fuel to the java pointer debate. I have seen more than one C++ developer seriously disappointed "because they told me java has pointers too".
I agree. autopointers are the reason to work with java. When you don't worry about memory you can really focus on the structure of the app.

But at the same time, I prefer c++ for high performance apps because finite pointer control allows for tight and fast code.
 
Old 11-14-2008, 03:40 PM   #8
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 131Reputation: 131
Quote:
They will also qualify for gc when they go out of scope as in the example in the OP's post. It is a static constructor that throws and exception, there for the values will sit at null even though the object was not created. There for the hanging static vars will be waiting for the gc to run or the vm to end because they are static so always in scope.
Surely you have to agree that I already said so in my first post, on the subject of the static variable.
 
Old 11-14-2008, 03:40 PM   #9
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by jay73 View Post
Which is what I already said in my previous post on the subject of the static variable.
Yeah, you posted that in an edit while I was already responding.
 
Old 11-14-2008, 03:43 PM   #10
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 131Reputation: 131
LOL, I have never been the fastest one in the world. Maybe LQ should transfer to java and start using locks and conditions.

But you are wrong about the edit because what I added is this:
Quote:
Null means it will get collected automatically, at some time.
Your post made me realize that my answer did not make any sense at all without this.

Last edited by jay73; 11-14-2008 at 03:46 PM.
 
Old 11-14-2008, 05:52 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
This is really a lot of discussion about nothing. In C and C++ the memory being used is provided by the process space directly, whereas in Java the JVM provides it. This means the real question is if the JVM is fallible in this sense, or if it has the foresight to have a GC scheme much more complex than the few features mentioned. As far as we're concerned, the JVM holds a valid pointer to everything in every running Java application; therefore, no leak can exist. Within the context of a specific Java v-process is a different issue, however.
ta0kira
 
Old 11-14-2008, 07:53 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ta0kira View Post
This is really a lot of discussion about nothing. In C and C++ the memory being used is provided by the process space directly, whereas in Java the JVM provides it. This means the real question is if the JVM is fallible in this sense, or if it has the foresight to have a GC scheme much more complex than the few features mentioned. As far as we're concerned, the JVM holds a valid pointer to everything in every running Java application; therefore, no leak can exist. Within the context of a specific Java v-process is a different issue, however.
ta0kira
I once was quite amused to see a job ad with the description requiring to deal with memory leak issues - in a Java shop.
 
Old 11-15-2008, 12:46 PM   #13
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
I once was quite amused to see a job ad with the description requiring to deal with memory leak issues - in a Java shop.
Actually there is a pretty big market for c/c++ devs that have good memory management skills to fix memory leaks in java code. At first it is a laugh to think of a memory leak in a java program, but then if you realize that you can't over run the systems memory but you can over run the jvm it seems apparent.

Bad use of statics and gc restrictions (which can be changed with param calls to the gc in code) all lead to a slow burn of memory in the jvm's pool and this must be managed but not as strongly as in other languages.

On large scale apps with lots of data stores this can be the down fall of a program very fast.
 
Old 11-15-2008, 01:04 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by xhypno View Post
Actually there is a pretty big market for c/c++ devs that have good memory management skills to fix memory leaks in java code. At first it is a laugh to think of a memory leak in a java program, but then if you realize that you can't over run the systems memory but you can over run the jvm it seems apparent.

Bad use of statics and gc restrictions (which can be changed with param calls to the gc in code) all lead to a slow burn of memory in the jvm's pool and this must be managed but not as strongly as in other languages.

On large scale apps with lots of data stores this can be the down fall of a program very fast.
An excellent ad for Java .
 
Old 11-15-2008, 02:56 PM   #15
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
An excellent ad for Java .
Actual
ly, my prefered language is still c++, but that is because I work daily with development for MPI apps for an HPC environment.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java - package does not exist error epoo Programming 2 06-05-2007 12:21 AM
Why Pointer is not use in java? hytechpro Programming 4 12-19-2005 09:18 AM
JAVA on firefox: PLugin directory does not exist !! DarkExile Linux - Newbie 4 01-03-2005 07:54 AM
Problem with crone - dangling job gstasica Linux - General 0 11-10-2004 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration