Memory Management

J

javajedi

Hi everyone,
My question is about memory management.
Here is a class:

public class A{
int a;
public int f(){ int x = 2;}

public static void main(){
A obj1 = new A();
A obj2 = new A();

}
From the example above, JVM will take place from heap for integer "a"
and for function "f" but when it comes to create the second object JVM
will take place for "a" again but what about the function "f". Does it
again take place for this function?
I read about method place in heap and I think it wont take place for
methods again but I want to be sure.

What do you think?
 
M

Mike Schilling

javajedi said:
Hi everyone,
My question is about memory management.
Here is a class:

public class A{
int a;
public int f(){ int x = 2;}

public static void main(){
A obj1 = new A();
A obj2 = new A();

}

When class A is loaded , the space required to hold its definition is
allocated. How much space that is is JVM-dependent, and I know of no good
way to calculate it. It is certainly larger with f() present than without
it, so there is a cost to unnecessary methods. (This space will be freed if
A is ever unloaded, but that's an advanced topic we'll skip over for now.)

When main() is run, space for an instance of A is allocated twice. Each
includes the space needed for an integer (a). Since, when main returns,
there are no references to obj1 and obj2, they are both eligible for garbage
collection. After being collected, the space allocated for them will be
freed and available for reuse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top