0

I have doubt in Java static method memory allocation. I have three classes like,

class a {
public static void a() {}
}
class b {
public static void a() {}
}
class c {
public static void a() {}
}

Here all three classes have a static method a() with same signature. My doubt is, How memory will be created here? I mean only one memory will be created for method a() in heap or three memory space?

Consider above three classes are tapestry pages. If the user entered the class A, the static method memory will be created/allocated and then I have redirect to class B, here also I have the same static method. My doubt is, here memory will be create or not for class B static method .

0

2 Answers 2

4

The compiler does not "de-duplicate" methods, if that is your question. Neither does the runtime (if anything, there is a performance optimisation of inlining method calls that sort of does the exact opposite).

If you have common code, it is up to you to refactor out the reusable parts and, well, re-use them.

Sign up to request clarification or add additional context in comments.

Comments

2

Here all three classes have a static method a() with same signature. My doubt is, How memory will be created here?

If you call these methods, they can be loaded into the Perm Gen or Metaspace.

I mean only one memory will be created for method a() in heap or three memory space?

No space is used on the heap. If you use all three methods each will be loaded into memory.

here memory will be create or not for class B static method .

More memory will be used but it will be only a few bytes, and only once. Unless you methods are huge, this won't make much difference.

8 Comments

Nitpicking: Isn't the method loaded when the class is loaded (even if it is never called)? And isn't the method definition and bytecode also placed into heap memory (same as the rest of the class)? Or is there (still?) a special area for it? (Of course, any native code hotspotted from there might go somewhere else)
@Thilo the byte code for a class can be loaded at once, though the method might only be extracted from the byte code when you call it for the first time (It could depend on the implementation). The definition and byte code are not cached on the heap and there is no way to get the original byte code for a class (you can get a copy you assume is the same, but it is another copy)
@Thilo you can try to get the byte code for a class by looking on the classpath, however you could load a different byte code for class using defineClass or Instrumentation and it's not possible to see what the resulting byteCode was (unless you are the last Instrumentation agent, and you keep a copy for later) Classes like lambdas and reflection handlers are generated on the fly and you can't retrieve them from the heap and don't appear in a heap dump.
But wasn't there the so-called "permGen" back in the day (part of heap) that would fill up when we had too many classes?
@Thilo PermGen is dead, Metaspace lives (not PermGen 2.0 ;) and if you set a maximum size for it, it will do exactly the same thing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.