-3

I am creating a GUI program in which there are several menus. By clicking the menu item, a new frame is opened and perform some task and then close it but not the main frame. For this reason, I created a separate class form.java. In this class, I created ten functions, in which each fn contain one frame. I created the object of this class in the constructor of my main class in which my main frame. I call that function when one menu item is clicked.

Question: Are all the functions using memory when only object is created of form.java class in my main class before calling that function? This function is called when the menu item is clicked. I suppose at that time it takes memory space and not before calling...

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
  • The obligatory comment: [The Use of Multiple JFrames: Good or Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) – Hovercraft Full Of Eels Jun 10 '16 at 17:08
  • It's unclear what you mean by "the function take memory." Every method belongs to a class, and it gets loaded into memory whenever its class object is loaded. Usually, that will be triggered by the first reference to any member (method, variable, or constructor) of the class. But each method activation also uses memory on a thread's call stack, and each method activation likely will create new objects that exist in memory. So I'm not sure which of those several ways of "taking memory" you are asking about. – Solomon Slow Jun 10 '16 at 17:22

2 Answers2

0

The child frame is created only when the function is invoked as a result of clicking on menu item. The memory would not be assigned for the child frames when object of your custom class i.e. form.java is created. This is because the child frame objects are local to the function/method declared inform.java. Memory would only be allocated for member variables of form.Java when form.java is constructed, not for its local variables. You can confirm this using the profiling tool VisualVM shipped with JDK.

0

Answer: NO MEMORY CONSUMPTION FOR OTHER MEMBERS LIKE methods/functions. Explanation:

Important : Function itself never take whole block of memory even when it is called.

So how it actually works ?

To understand this, you should have some knowledge on System Programming And Compiler Construction.

There is a concept of Active Records. Active Records are nothing but memory mapping details including static and dynamic memories.

In JVM, When an object is Created, it occupies a memory in STACK AND HEAP. Now if there is any possible path in stack to access heap location, then this is actually supposed to be consuming memory.

Now if an Object of a class is invoked/created, it occupies memory only for the reference variable(i.e. before new) and also its instance variables occupies, and NO MEMORY CONSUMPTION FOR OTHER MEMBERS LIKE methods/functions.

miiiii
  • 1,580
  • 1
  • 16
  • 29