Connecting Tech Pros Worldwide Forums | Help | Site Map

objects and garbage collection

Newbie
 
Join Date: Jun 2008
Posts: 2
#1: Jun 27 '08
class C
{
public static void main(String args[])
{
C c1 = new C();
C c2 = m1(c1);
C c3 = new C();
c2 = c3; //6
anothermethod();
}
static C m1(C ob1)
{
ob1 = new C();
return ob1;
}
}

After line 6, how many objects are eligible for garbage collection?

Ans: 2

Please tell me as to what are those two objects here and how?

Thanks in advance...

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 27 '08

re: objects and garbage collection


I'd say only one object can go; the one created by the m1() method and previously
assigned to variable c2. Why do you think two objects are garbage?

kind regards,

Jos
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#3: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by JosAH

I'd say only one object can go; the one created by the m1() method and previously
assigned to variable c2. Why do you think two objects are garbage?

kind regards,

Jos

This confuses me as well. Why is c1 not eligible for garbage collection? anotherMethod() must be static, so after the method is completed the program will exit and remove c1 and the pointer to c2 and c3 from memory, no?

Just so you know I have absolutely no expertise when it comes to garbage collection so I'm asking out of curiosity.

-blazed

Edit: And about the previous thread, I always thought java had no pointers and only references, so it always copied the memory to a new location when using object1 = object 2. I was clearly mistaken...
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by blazedaces

This confuses me as well. Why is c1 not eligible for garbage collection? anotherMethod() must be static, so after the method is completed the program will exit and remove c1 and the pointer to c2 and c3 from memory, no?

Yep, but the question was how many objects can be garbage collected just after
the assignment c2= c3, i.e. *before* that other method was called.

Quote:

Originally Posted by blazedaces

Edit: And about the previous thread, I always thought java had no pointers and only references, so it always copied the memory to a new location when using object1 = object 2. I was clearly mistaken...

References and pointers are similar here. Jim Gossling choose to use the noun
'reference' instead of 'pointer', maybe because Java doesn't have any pointer
arithmetic as C does.

Fact remains that the OP is making this all far more complicated than it actually
is; only the object previously pointed to by c2 can be safely removed.

kind regards,

Jos
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#5: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by JosAH

Yep, but the question was how many objects can be garbage collected just after
the assignment c2= c3, i.e. *before* that other method was called.



References and pointers are similar here. Jim Gossling choose to use the noun
'reference' instead of 'pointer', maybe because Java doesn't have any pointer
arithmetic as C does.

Fact remains that the OP is making this all far more complicated than it actually
is; only the object previously pointed to by c2 can be safely removed.

kind regards,

Jos

Alright, then this all makes a lot of sense.

And the psuedo-reference pointer also makes sense as an extremely efficient way to have different variables, but still save space.

So may I ask, if you do the following:

Expand|Select|Wrap|Line Numbers
  1. String test1 = new String("testing1");
  2. String test2 = test1;
  3. test2 = test2 + test1;
  4. System.out.println("test1: " + test1);
  5. System.out.println("test2: " + test2);
  6.  
What exactly happens in the lower level code. At line two (String test 2 =test1) test2 point to the same memory location as test1, correct? But then, after line 3, does test2 point to a new location in memory? Does memory get reallocated every time an object is changed?

Or does the concatenation do something like simply adding character bytes to the end of the sequence of bytes, so previously test2 pointed to character bytes 0-7 (what test1 points to), but afterwards it points to 0-15?

-blazed
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#6: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by blazedaces

Expand|Select|Wrap|Line Numbers
  1. String test1 = new String("testing1");
  2. String test2 = test1;
  3. test2 = test2 + test1;
  4. System.out.println("test1: " + test1);
  5. System.out.println("test2: " + test2);
  6.  
What exactly happens in the lower level code. At line two (String test 2 =test1) test2 point to the same memory location as test1, correct?

Correct, or rather (since references may not hold something as implementation-dependent as a "memory location") test1 and test2 reference the same object.

And by the way, it simpler to write:
Expand|Select|Wrap|Line Numbers
  1. String test1 = "testing1";
Quote:

Originally Posted by blazedaces

But then, after line 3, does test2 point to a new location in memory? Does memory get reallocated every time an object is changed?

Not in general, you're dealing with the specific case of strings. That assignment is equivalent to this code:

Expand|Select|Wrap|Line Numbers
  1. StringBuilder b = new StringBuilder();
  2. b.append(test2);
  3. b.append(test1);
  4. test2 = b.toString();
Quote:

Originally Posted by blazedaces

Or does the concatenation do something like simply adding character bytes to the end of the sequence of bytes, so previously test2 pointed to character bytes 0-7 (what test1 points to), but afterwards it points to 0-15?

-blazed

That's what is happening in the StringBuilder. The "+" notation is a sort of "syntactic sugar" to make the code look sweeter.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by blazedaces

Alright, then this all makes a lot of sense.

And the psuedo-reference pointer also makes sense as an extremely efficient way to have different variables, but still save space.

So may I ask, if you do the following:

Expand|Select|Wrap|Line Numbers
  1. String test1 = new String("testing1");
  2. String test2 = test1;
  3. test2 = test2 + test1;
  4. System.out.println("test1: " + test1);
  5. System.out.println("test2: " + test2);
  6.  
What exactly happens in the lower level code. At line two (String test 2 =test1) test2 point to the same memory location as test1, correct? But then, after line 3, does test2 point to a new location in memory? Does memory get reallocated every time an object is changed?

Or does the concatenation do something like simply adding character bytes to the end of the sequence of bytes, so previously test2 pointed to character bytes 0-7 (what test1 points to), but afterwards it points to 0-15?

-blazed

The lazy answer is: turn that code snippet into a simple class, compile it using
javac so that you have a .class file and then use javap, the disassembler and
see what actually happens. Use javap -c YourClass and see the results.

Java isn't playing any tricks on you, i.e. if you add two strings using the + operator
the compiler generates code for the StringBuilder that actually adds those two
Strings together and produces the result. Have a look at the output of javap and
see for yourself. btw javap is one of the tools that come with your jdk distribution.

kind regards,

Jos
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#8: Jun 27 '08

re: objects and garbage collection


The StringBuilder class! I've always used StringBuffer... Good to know this exists since it seems even more efficient (faster implementation according to the API).

Well, it's good that I now know better exactly how Java works. I should probably ask in the python section how it works exactly in python, but I assume it's similar.

-blazed
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#9: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by JosAH

The lazy answer is: turn that code snippet into a simple class, compile it using
javac so that you have a .class file and then use javap, the disassembler and
see what actually happens. Use javap -c YourClass and see the results.

Java isn't playing any tricks on you, i.e. if you add two strings using the + operator
the compiler generates code for the StringBuilder that actually adds those two
Strings together and produces the result. Have a look at the output of javap and
see for yourself. btw javap is one of the tools that come with your jdk distribution.

kind regards,

Jos

Well, I'm having a little bit of trouble. I've always used JCreator or some kind of IDE to use java and compile files/projects. So for some reason or another besides the "java" none of the other packages are available when I go to the command prompt (in windows, maybe I should switch to linux? My computer is dual boot). So javap and javac produce a response of "'javac' is not recognized as an internal or external command, operable program of batch file".

That stinks. I always assumed they were automatically implemented when I installed java. Great. Now to look up how to make them work. Unless you can tell me a way to do it through my IDE. I can add the "compiler" javap as a tool, but I can't figure out a way to use it when I click run instead of using the default other than changing the default compiler...

-blazed
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#10: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by blazedaces

Well, I'm having a little bit of trouble. I've always used JCreator or some kind of IDE to use java and compile files/projects. So for some reason or another besides the "java" none of the other packages are available when I go to the command prompt (in windows, maybe I should switch to linux? My computer is dual boot). So javap and javac produce a response of "'javac' is not recognized as an internal or external command, operable program of batch file".

That stinks. I always assumed they were automatically implemented when I installed java. Great. Now to look up how to make them work. Unless you can tell me a way to do it through my IDE. I can add the "compiler" javap as a tool, but I can't figure out a way to use it when I click run instead of using the default other than changing the default compiler...

-blazed

Program java.exe is in the bin of your JRE, while javac.exe and other tools are in the bin folder of your JDK. You need to either type in the absolute path of the program, or edit your PATH environment variable:

http://java.sun.com/docs/books/tutor...ems/index.html
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#11: Jun 27 '08

re: objects and garbage collection


Quote:

Originally Posted by BigDaddyLH

Program java.exe is in the bin of your JRE, while javac.exe and other tools are in the bin folder of your JDK. You need to either type in the absolute path of the program, or edit your PATH environment variable:

http://java.sun.com/docs/books/tutor...ems/index.html

I've edited my path file and can now run both just fine from command prompt. But now after I run javac and make the class file, even though I know the file is there, when I try to run javap -c "classname.class" it spits out "ERROR: Could not find TestProgram.class"

Any ideas?

-blazed
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#12: Jun 27 '08

re: objects and garbage collection


Like java.exe, javap takes the name of the class, not the filename:

Expand|Select|Wrap|Line Numbers
  1. javap -c YourClass
or

Expand|Select|Wrap|Line Numbers
  1. javap -c com.acme.widget.YourClass
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#13: Jun 27 '08

re: objects and garbage collection


Thanks man, and here's the output:
Expand|Select|Wrap|Line Numbers
  1. Compiled from "TestProgram.java"
  2. public class TestProgram extends java.lang.Object{
  3. public TestProgram();
  4.   Code:
  5.    0:   aload_0
  6.    1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
  7.    4:   return
  8.  
  9. public static void main(java.lang.String[]);
  10.   Code:
  11.    0:   new     #2; //class java/lang/String
  12.    3:   dup
  13.    4:   ldc     #3; //String testing1
  14.    6:   invokespecial   #4; //Method java/lang/String."<init>":(Ljava/lang/Strin
  15. g;)V
  16.    9:   astore_1
  17.    10:  aload_1
  18.    11:  astore_2
  19.    12:  new     #5; //class java/lang/StringBuilder
  20.    15:  dup
  21.    16:  invokespecial   #6; //Method java/lang/StringBuilder."<init>":()V
  22.    19:  aload_2
  23.    20:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/
  24. String;)Ljava/lang/StringBuilder;
  25.    23:  aload_1
  26.    24:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/
  27. String;)Ljava/lang/StringBuilder;
  28.    27:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/la
  29. ng/String;
  30.    30:  astore_2
  31.    31:  getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;
  32.    34:  new     #5; //class java/lang/StringBuilder
  33.    37:  dup
  34.    38:  invokespecial   #6; //Method java/lang/StringBuilder."<init>":()V
  35.    41:  ldc     #10; //String test1:
  36.    43:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/
  37. String;)Ljava/lang/StringBuilder;
  38.    46:  aload_1
  39.    47:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/
  40. String;)Ljava/lang/StringBuilder;
  41.    50:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/la
  42. ng/String;
  43.    53:  invokevirtual   #11; //Method java/io/PrintStream.println:(Ljava/lang/St
  44. ring;)V
  45.    56:  getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;
  46.    59:  new     #5; //class java/lang/StringBuilder
  47.    62:  dup
  48.    63:  invokespecial   #6; //Method java/lang/StringBuilder."<init>":()V
  49.    66:  ldc     #12; //String test2:
  50.    68:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/
  51. String;)Ljava/lang/StringBuilder;
  52.    71:  aload_2
  53.    72:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/
  54. String;)Ljava/lang/StringBuilder;
  55.    75:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/la
  56. ng/String;
  57.    78:  invokevirtual   #11; //Method java/io/PrintStream.println:(Ljava/lang/St
  58. ring;)V
  59.    81:  return
  60.  
  61. }
  62.  
...

Did I do this just to verify what was said above about what java is actually doing with the "+" operator on strings? Good to know... but this was never an issue of mine.

Thank you much for your help and knowledge.

-blazed
Reply