• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Memory Usage in static Method

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have following questions for the above scenario:

Q1.: When will class B comes in memory..is it when A is calling static method of B?
Q2.: How I can release the memory of this class B as after getting the return value it is no more used.
Q3.: Is it that when garbage collector will run it release this class memory as this is no more in use? Does it mean that gc() release those objects also which are not used for sometime.
regards,
Arun
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi arun,
Here are the answers :
(1) Yes. Class is loaded in memory when there is reference to any of its static fields/methods or when an instance of class is created.
(2) The memory occupied by class B would be reclaimed by Garbage collector as there is no reference to any object of class B.
(3) Yes. GC will release memory of those objects for which there does not exist any reference. When would the garbage collector run is either when your jvm is falling short of memory allocated to it by cpu or when you force the GC to run ( though it does not relent to any force, I suppose )
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer...tha which is preferable & efficent:
Method 1 or Method 2. or both are same?:-
Method1

Method2
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except that 2) wouldn't compile, 1) would probably be slightly more efficient regarding runtime performance.
But why do you care? Do you have encountered an actual performance/memory usage problem?
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ilja for the reply..No I am not facing any performance problems but perhaps just was more worried on it..Thanks anyway..
One thing I was not able to understand that why Method 2 won't compile..perhaps it is not properly written i modeified it in following way and found working..Is that was the only reason or you want to point out something special..
Regards,
Arun
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by arun mahajan:
Thanks Ilja for the reply..No I am not facing any performance problems but perhaps just was more worried on it..Thanks anyway..


You should probably be more worried about maintainability and extensibility...
Time to reiterate the Three Rules Of Optimization:
1. Don't
2. (for experts only) Don't - yet
3. (if and only if you really need to optimize) Use a profiler to find the actual bottleneck - it isn't where you think it is.


One thing I was not able to understand that why Method 2 won't compile..perhaps it is not properly written i modeified it in following way and found working..Is that was the only reason or you want to point out something special..


In your original post, you forgot the method body in class A - that's all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic