-1

I have referenced this link. Heap memory

I have a doubt

class Sample
{
    public static void main(String[] args)
    {
        Date dt=new Date();
        System.out.println(""+dt);
    }
}

First dt will be at eden space if GC happens it will go to Survivor space , if GC happend when dt is in Survivor space (correct me if am wrong).

My Question is:

  1. if date is displayed and program completes the running process then will these dt remains in Heap space ??
  2. If GC not occured where dt will remains ??
    (eden space, survivor space or etc)
  3. program completes the running process then dt will have no reference am i right??

Thank you very much .....

Community
  • 1
  • 1
Amith
  • 1,907
  • 5
  • 29
  • 48
  • After the program completes, it doesn't exist in memory anymore, so your questions are kinda pointless... – vanza Jul 31 '12 at 03:43
  • @vanza will object removes from heap memory when program completes without GC ??? – Amith Jul 31 '12 at 03:47
  • When processes exit the OS reclaims any memory that was allocated for them. They exited; they don't exist anymore. It's how OSes work. – vanza Jul 31 '12 at 03:47
  • @vanza so Heap is a space for a temporary storage of an object. heap will get free after program exit ??? if the heap memory is full (While running the program) GC removes the non referenced object of class(currently running ) ??? there is no object of previously running class ???? – Amith Jul 31 '12 at 03:55

1 Answers1

1

if date is displayed(program completes the running process) will these dt remains in Heap space ??

(1) When the program completes there will be nothing on heap (dt will not exist)

If GC not occured where dt will remains ?? (eden space, survivor space or etc)

(2) All you can say about "dt" is that it will be created in eden space, After that if it goes to survivor space or Tenured Generation or gets garbage collected is all managed by GC and you cannot exactly know where it

program completes the running process dt will have no reference am i right??

(3) Yes it wont have any ref. In fact when the method terminates dt will be eligible for GC (now if gc happens rt away or takes time you don't know)

To be clear, There are NO guarantees with GC, any memory allocated during program execution might remain allocated after program termination, in such cases it will be reclaimed by the operating system

Vivek
  • 1,316
  • 1
  • 13
  • 23
  • Thanks vivek .... please clarify these too ..... so no object of any class Remains in heap when program completes running process (in any exceptional case) – Amith Jul 31 '12 at 04:42
  • @Amith To be clear, There are NO guarantees with GC, any memory allocated during program execution might remain allocated after program termination, in such cases it will be reclaimed by the operating system. – Vivek Jul 31 '12 at 05:20
  • @Amith here is a good article on etutorial http://etutorials.org/Misc/programmers+guide+java+certification/Chapter+8.+Object+Lifetime/8.1+Garbage+Collection/ Hopw this will help clarify most of the doubts. – Vivek Jul 31 '12 at 06:41