Solved

Stack and Heap memory in Java

Posted on 2009-07-11
10
4,852 Views
Last Modified: 2013-11-23
I am compiling few questions at one place. Can someone please give me answers?

1) I have class A and class B. if I create a instance of B in class A's method, where the memory will be created?
2) where the static methods memory will be created?
3)where the singleton class memory will be created and when will it be destroyed?
4)what is the difference between static class and singleton class. what is the base to choose?
0
Comment
Question by:glimlach
  • 6
  • 4
10 Comments
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 500 total points
Comment Utility
1) always on the heap.
2) local variables of ALL methods live always on the stack.
3) always on the heap and destroyed on an UNCERTAIN moment by the garbage collector.
4) a static can not be static, you mean a static reference to a class.
Single ton is not a special instance but a choice of design.

;JOOP!
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
Comment Utility
Additional:
1) you can see JAVA as a stock market: what ever you buy or sell,
you won't ever have the shares in your hands, only papers that tell you
how much you got.
2) static methods are just methods.
They have a different usage and deployment, but all methods are the same in nature.
3) a singleton item is an item that you prevent from being created in large numbers.
A simple example is a class with a private constructor and a public 'getInstance()'
method that controls the limited instantiation.
So ............. it's just a class.
Destroyed memory does not exist:
it is released memory and it will be made available to the empty heap,
when the garbage collector 'sees' urgence (no more free memory) or when there's lots of time available.
You should not worry about it.
4) a singleton class must keep track of its only(!) instance, without the need to create one; then you must use a static reference within the class to tell what you've got.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
.................. I fear I'd rather told you in Dutch ...............................................
;JOOP!
0
 

Author Closing Comment

by:glimlach
Comment Utility
I am sorry, I may not understand your answers correctly. My questions are related to memory creation and memory destroy.
Q1. If creates on Heap then when will it destroys? in the first question, if it creates in Heap, if i also have one variable in that menthod, then where will create? will it create in Stack? what is the difference in terms of creation and memory destroying?
Q2. How can we access static methods without creating a class? will memory of those methods creates a global memory? till what time it will exists? will the memory creates at the time of calling and destroys it as soon as static method execution completes? if so, what is the difference with normal method than static method?
Q 3 & 4:. Singleton object available till the context is alive. then what is the difference between static objects memory and singleton object memory? when should i choose singleton class and when should i write a class with all static methods?
0
 

Author Comment

by:glimlach
Comment Utility
I am sorry, I may not understand your answers correctly.
My questions are related to memory creation and memory deletion.
Q1. If it creates on Heap then when will this memory getdestroyed? in the first question, if it creates in Heap, if I also have one variable (eg: int )created in that method, then where will the memory for this "int" variable be created? will it create in Stack? what is the difference in terms of these memory creation and memory destroy mechanism?
Q2. How can we access static methods without creating a class? will memory of those methods created at global memory location? till what time it will exists? will the memory creates at the time of static method calling and destroys it as soon as static method execution completed? if so, what is the difference with normal methods than static methods?
Q 3 & 4:. Singleton object available till the context is alive. then what is the difference between static objects memory and singleton object memory? when should I choose singleton class and when should i write a class with all static methods?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Q1. You will NOT know exactly when the 'destroyed' object will be 'finalised()' and
added to the free list.
Local variables in methods are ALWAYS created on the thread's stack.
The stack area is released EXACTLY at the return from method.
Q2.You MUST access a static method without creating an object, or ignoring
an existing one by calling:   class.method();
You will not create global memory by calling static methods.
There is no difference between the local memory of methods of any kind.
Q34: a class with only static methods is only a place holder for global methods.
E.g.: the Math class.
;JOOP!

P.S.: why did you close and give points if you go on asking questions?
0
 

Author Comment

by:glimlach
Comment Utility
Sorry I did it mistakenly. I was not clear about the answers after that.
Thanks for the reply. however I still have few doubts on the same issue.
your answers: 1)always on the heap.
2) Local variables in methods are ALWAYS created on the thread's stack.
My questions:: what does it mean? I read that threads will access same heap memory but not stack memory.
public void test(){
B b = new B()
}
If i create B object inside a method and if two threads access this method one by one. then two variables will be created separately. if threads access same heap memory, it should use the single object right? but is it so happening in the above example if two threads access one another?

Your Answers:
You MUST access a static method without creating an object, or ignoring
an existing one by calling:   class.method();
My Questions:
I know that. Buy Why should call like that? why should i create static methods instead of normal method that invoking with object instance?

Your Answers:
a class with only static methods is only a place holder for global methods.
E.g.: the Math class.
My Questions:
I know that. That is not my question. Ok forget about memory creation part.
Singleton object available till the context is alive. then what is the difference between static objects  and singleton object? (I know how to create singleton class)
There are classes with all static methods and there are singleton classes.  when should I choose singleton class and when should i write a class with all static methods? and Why?
AND how the memory creation and it's life time of these two type of classes happens.
Sorry dont mind. I know java well how to create static classes, singleton classes, design patterns etc. but I am confused about internal mechanism of how they are creating, location, when do we have to write static class and singleton classes
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
As 2 threads must be able to call methods in parallel, they have their own stack area.
And they will create separate objects in the same heap area.

You need to create a static method if you got no object (yet).
And in case of a singleton class you MUST call a static method because
you may not call the constructor.

>>> There are classes with all static methods and there are singleton classes.  when should I choose singleton class and when should i write a class with all static methods? and Why?

The Math class answers one question, a GUI creating class for your GUI should
be a singleton for obvious reasons.
And the two have no relationship.

The Math class has no life time, it is there all the time: no objects.
A Singleton may only be destroyed if the last (and only) reference to it
goes to null; that reference will also be a static variable inside the class,
so you need a static method 'destroy()' or such.

You may give me another 1000 points for this (reopen the question and give it A level).

;JOOP!
0
 

Author Comment

by:glimlach
Comment Utility
ok. I'll re open and give rating if you give me answers that wont create me any more questions.

The Math class has no life time,
 it is there all the time:-- Where Sir?

 I dont think your answers for last 3 answers are really having complete/clear answers. your explanation (eg: You need to create a static method if you got no object (yet).) is correct but not answers to my questions.

Your first answer was pretty clear. Thank you.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
All classes that are loaded into the java virtual machine 'live' in  the class administration (only).

This is a simple singleton class:

public class Singleton
{
     private Singleton(){ ................ }   // inaccessible constructor.
     private static Singleton me = null;  // reference to the olny(!) instance.
     public static Singleton getInstance()
     {
           if(me == null) { me = new Singleton(); }
           return(me);
     }
     public static destroy()
     {
            me = null;
     }
}

;JOOP!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Issues installing SSL certificate into Apache Tomcat 3 70
allStar challenge 1 40
pairs challenge 5 44
HashMap Vs TreeMap 12 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now