• Post Reply Bookmark Topic Watch Topic
  • New Topic

JVM Garbage Collection  RSS feed

 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have one question regarding JVM garbage collection. The scenario is like this: I created a list storing 10 data objects, some time after, the list itself is no longer referenced, but 7 out of 10 data objects are still referenced by other objects, but the other 3 are no longer referenced by any other things.

So my question is if the JVM will garbage collect these 3 data objects which are no longer referenced.
 
Simon Baker
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think that if the list is dereferenced, and there are no other references to the 3 objects, they would become candidates for GC. The other 7, being referenced elsewhere (presumably from "live" objects) would not be candidates for GC. This may be a simplistic view, however.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. There's nothing special about references that happen to be in a Collection. An object is eligible for GC once there are no strong(*) references to it.

(*) All references you normally see in a simple Java program are strong references. The other types are soft, weak and phantom references. However, you won't see these in simple programs. And, when they are used, they are explicit: via a SoftReference, WeakReference or PhantomReference object.
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies, but it seems the GC doesn't work. I wrote a simple program to test it. I created 2 timer task. One for adding data objects into a list in a hashmap, and a few into another list. The other for clear the hashmap and call GC and wait for some seconds. I know the gc call only suggests the JVM to do the garbage collection. But why it doesn't do anything even the memory usage is very high and finally out of memory?

Please find the code below for your reference:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what datalist2 is for in this example; but all but 100 of the Strings get stored into it each time the timer goes off, and it never gets cleared. The Strings added to this list will eventually fill up all memory.
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the dataList2 is holding a few data objects which are the same in dataList1. But only a few, 100 Strings.

Try to run my program, you will find for each 100,000 strings added into dataList1, the memory goes up by 5MB, and after the call of GC, the memory doesn't go down (which is expected, right? The memory should go down sharply because dataList2 only holds a few reference.), So it continues to go up, and finally, not for long, it is crashed.

So as I said before, the question is if the JVM will really release the memory hold by dataList1 if a few (100 strings) are still held by dataList2?

I tried to comment the line out, and finally I find the answer.

The JVM will not release the data objects in the dataList1 if dataList2 holds a few references of the data objects in dataList1 even if most of the data objects in dataList1 are no longer referenced.

We can try the program, you will see the memory keeps going up and finally out of memory in a short time if you don't comment out the line . But if you do, it will never out of memory because the GC works.

Anyway, thanks everybody for helping me on this.
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if it is like this, how to collect back the never referenced data objects in dataList1?

I tried to use the clear method in the second timer task to clear the content in the dataList1, but it still doesn't work. The memory still goes up. It seems now the only option left for me is not to add those 100 strings into dataList2, but it's not possible for my application.

HELP ME!!
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, guys, my fault. One defect in my test program.


Which adds most of the string data into the dataList2. So they are not get released.

I tested it again. The JVM does garbage collect those data objects no matter where they are.

 
Simon Baker
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not executed the code - these comments are just from reading it - but it looks as though your datalist2 will, after the AddDataTimerTask.run() method, contain a reference to an array holding references to all your created String objects.

Perhaps the code:

is incorrect? The reference to the array is a reference to the array regardless at what point the reference was created.
 
Simon Baker
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think our last two posts were made simultaneously - a bit of a thread synchronization issue there
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mingwei Jiang:
Sorry, guys, my fault. One defect in my test program.


Yes, that's what I told you roughly nine hours earlier. Would have made sense to believe me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!