473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

objects and garbage collection

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...
Jun 27 '08 #1
12 1820
JosAH
11,448 Expert 8TB
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
Jun 27 '08 #2
blazedaces
284 100+
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...
Jun 27 '08 #3
JosAH
11,448 Expert 8TB
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.

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
Jun 27 '08 #4
blazedaces
284 100+
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
Jun 27 '08 #5
BigDaddyLH
1,216 Expert 1GB
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";
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();
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.
Jun 27 '08 #6
JosAH
11,448 Expert 8TB
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
Jun 27 '08 #7
blazedaces
284 100+
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
Jun 27 '08 #8
blazedaces
284 100+
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
Jun 27 '08 #9
BigDaddyLH
1,216 Expert 1GB
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
Jun 27 '08 #10
blazedaces
284 100+
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
Jun 27 '08 #11
BigDaddyLH
1,216 Expert 1GB
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
Jun 27 '08 #12
blazedaces
284 100+
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
Jun 27 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: EKL | last post by:
What do I have to do to guarantee that the objects that my code creates are reclaimed by only minor garbage collection runs? In other words, what do I have to do to only generate garbage for minor...
9
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using...
2
by: Mel | last post by:
This may be a stupid question, but here goes... I have created a NameValueCollection in my website's application state. If, during a page request, I add a string key and string value to the...
2
by: elaine | last post by:
I'm working on a .net web application. The architect of this web application is quite different than other web applications i worked before. Since we use a set of tools to generate most of the...
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.