1

As far as I know, for space in old generation of JVM, it could be utilized for two purposes,

  1. used for objects promoted from young generation to old generation?
  2. used for new object allocation in special use cases (https://stackoverflow.com/questions/9053144/will-i-encounter-java-lang-outofmemoryerror-even-with-no-lack-of-memory)

My questions are,

  1. Are there any other use cases to which will utilize space in old generation?
  2. I think there is a memory copy involved to copy objects from young generation to old generation, is it a deep copy or a shallow copy?

thanks in advance, Lin

Community
  • 1
  • 1
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • is your memory showing continuous increase? are you using any static Maps or something? – Narendra Pathai Dec 02 '12 at 15:42
  • Hi @Narendra, I am trying to figuring out some basics of GC since I may need to tune a write heavy application. – Lin Ma Dec 02 '12 at 15:52
  • ok. Because memory leaks cause continuous increase in Old generation. That's why such a question – Narendra Pathai Dec 02 '12 at 15:56
  • Hi @Narendra, what do you mean "memory leaks cause continuous increase in Old generation"? Is there a reference document? – Lin Ma Dec 02 '12 at 16:03
  • Don't have any reference but in memory leak the the objects are not freed during GC cycle and they keep getting promoted and will lead in filling of Old generation eventually. – Narendra Pathai Dec 02 '12 at 16:09
  • Thanks @Narendra, for objects promoted to old generation, I think it is still part of JVM heap, why do you think it is leak? – Lin Ma Dec 03 '12 at 12:57
  • Leak because for you the references have become unreachable but due to some unknown static references to the same object somewhere, they will never be garbage collected. And due to that slowly you will lead to the dreaded OutOfMemoryError and application will crash – Narendra Pathai Dec 03 '12 at 13:00
  • @Narendra, what do you mean "references have become unreachable"? Unreachable by any root object? If so, such references should be root? Please feel free to correct me if I am wrong. If you could show me some code, it will be great since 10-line code is much clear than 1,000 words. We can start a new thread then with your samples. – Lin Ma Dec 03 '12 at 13:09

2 Answers2

1

Let me answer 2. It is definitely not a deep copy: the GC handles objects as distinct memory blocks and deep copying really only means copying many distinct objects that are connected into an object graph. Also, you'd be better served by imagining a move, not a copy; copying is in fact just an "implementation detail" and the desired effect is relocating.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Thanks @Marko, yes, I agree that using move is a better choice. Actually my question is, 1. it is really moved as a whole, or just move a reference. For example, there is an object which is a buffer containing 1M characters, do we move the whole 1M characters when promote the buffer object from young generation into old generation, 2. supposing there is an object which is promoted, do we move its whole object graph (even if some objects on the graph is not qualified for promotion)? If not, GC handles fixing object reference update issue in the object graph? – Lin Ma Dec 02 '12 at 15:50
  • Just forget about encapsulation when considering the GC. It sees you rbuffer object as just a small struct and sees the 1M array as a separate block. They are completely independent of one other. Note that fixing up pointers is always a requirement, whether you move the object graph or just the root object. Pointers are not relative. And yes, that whole array will be relocated (copied) when promoting to the Tenured generation, unless it's so huge that it gets allocated there right at the start. – Marko Topolnik Dec 02 '12 at 15:54
  • Thanks for the detailed deply, @Marko. Let me confirm my understanding is correct by repeating my understanding in my words, 1. reference/pointer are fixed by GC 2. GC will treat each object independently (to promote or move), other than treat object graph as a whole to promote (or move)? – Lin Ma Dec 02 '12 at 16:07
  • BTW: what is your comments to my question #1 in the original question? :-) – Lin Ma Dec 02 '12 at 16:09
  • 1
    Yes, you are getting this right. As far as your #1 question, I have no knowledge of any other routes to memory allocation in the tenured generation, but I don't know for a fact that there is no other way. – Marko Topolnik Dec 02 '12 at 16:17
  • Hi @Marko, I have a related question, and not sure if you have any advice => http://stackoverflow.com/questions/13683866/garbage-collection-issue-in-young-generation, thanks. – Lin Ma Dec 07 '12 at 13:37
1

Placing this code in response to the question by @Lin Ma in his comment:

class SomeClass{
    private static List<Object> memoryLeakCulprit = new ArrayList<Object>();



    void someMethod(Object obj){

          //adding the reference of some object in the culprit list
          memoryLeakCulprit.add(obj);

    }

    //supposed to remove the reference passed
    void someOtherMethod(Object obj){

         obj = null;

         //bummer forgot to remove the reference from list

         //now the instance is not reachable by obj
         //so now as new references are added to culprit list the memory will keep on increasing in size

    }

}

UDPATE

How to solve this leak

oid someOtherMethod(Object obj){

              //before setting the obj reference to null it must be removed from culprit list
              memoryLeakCulprit.remove(obj);

              //now its safe to set this reference to null
              obj = null;

        }

Only way to solve the leak it to Profile the application using some profiling tools such as JProfiler, VisualVM and find out which class is causing the leak.

When you find the class, THE CODE WILL HAVE TO BE CHANGED and that is the only way.

There is no need to free the references before the program exiting. Reason is that static variables (memoryLeakCulprit) are bound to Class object, and as soon as you exit the program all the references are automatically freed including the Class Object.

On totally other note always make sure to close System Resources (Sockets, DB connections) before exiting the program.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • Hi @Narendra, I think in your sample, the object is referred by memoryLeakCulprit, so the object is not GCed? I think it is expected that object is not GCed, when there are any references, why do you think there is a leak? Thanks. – Lin Ma Dec 03 '12 at 16:16
  • @LinMa I think you are misunderstanding Leak, It is a programming mistake that is seen to take place many times. As here it is just a single small program it is easy to pin point. But when there are 1000K LOC then it is not that easy. Thats why people use Heap Dumps and JConsole type of applications to find leaks. It is a programming mistake! – Narendra Pathai Dec 03 '12 at 16:45
  • 1
    Quoting Wikipedia: A memory leak, in computer science (or leakage, in this context), occurs when a computer program acquires memory but fails to release it back to the operating system.[1] In object-oriented programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running code. – Narendra Pathai Dec 03 '12 at 16:46
  • Hi @Narendra, good point. How do you think we should fix this issue? i.e. I think for "forgot to remove the reference from list", you mean Arraylist.remove (obj)? – Lin Ma Dec 06 '12 at 01:30
  • Another question is, for static reference memoryLeakCulprit itself, how to release it before end of the program? – Lin Ma Dec 06 '12 at 01:34
  • Thanks for the detailed update, @Narendra! I think the static reference (which is not released until program exits) is only the list object itself, not for all the contained object, correct? – Lin Ma Dec 06 '12 at 11:02
  • 1
    @LinMa yes if you remove the references from the static list, then only the List would remain till program exit and others will be freed. Great I think now you got it!! Still if you have something in mind, always love to get involved in constructive discussions. :) – Narendra Pathai Dec 06 '12 at 13:31
  • Thanks @Narendr, without your great example and description, I cannot figure out so quickly. You should have a party and write a book. :-) – Lin Ma Dec 06 '12 at 17:14
  • 1
    @LinMa just pass it to someone like you and that would be equivalent to partying :) Cheers! – Narendra Pathai Dec 06 '12 at 18:35