How much memory do Objects use?

I'm not sure how to ask this, but how does java manage memory usage for multiple objects? Is the memory needed for a classes methods shared amongst the objects of that class, or does each object have memory for it's methods allocated for its use alone?
For example:
class Foo {
   public void doSomething();
}Let's say I have 30 instances of Foo. I can think of 2 options (but I maybe wrong)
Case A: Each instance of Foo has it's own memory allocated for it's own copy of doSomething(), resulting in the total memory
usage of 30 instances of Foo being about 30 times how much memory 1 Foo uses
Case B: Each instance of Foo looks at a common copy of doSomething(), resulting in less memory needed since there is only one copy of doSomething in memory.
If the case is A, Static methods would seem to be potentially more memory efficient. Assuming my thinking is correct(and feel free to say I'm dead wrong), would it not make sense to make as many functions as possible static?

jverd wrote:
Executable code (initializers, methods, constructors): One copy of the bytecodes per ClassLoader that loads the class. Static/non is irrelevant.Seems to answer the question in full.
With reference (no pun intended) to the subject.
Instance variables (non-static member variables): One copy of the variable per instance of the class. Stored on the heap.
Class variables (static member variables): One copy of the variable per ClassLoader that loads the class. Stored on the heap.
Local variables, including parameters: One copy of the variable per invocation of the method.Just to complete the picture a bit (but NOT to contradict earlier comments).
Primitive variables - as above.
Reference variables - as above. Note that method local reference variables are stored on the stack but
any object instances referenced by these are stored on the heap even so.

Similar Messages

  • How much memory an object consumed?

    Via making an internet lookup and writing some code, i made an implementation of
    how to calculate how much memory an object consumes. This is a tricky way.
    However, how can i achieve a better and direct way? Is there a tool for attaching
    to JVM and viewing the memory locations and contents of objects in memory?
    I wrote the tricky way to Javalobby and pasting the same explanation to here too.
    I must say that this tricky way does not belong to me, and i noted where i took
    the main inspiration etc. at the end.
    Because of the underlying structure Java does not let
    users to access and directly change the content of
    instances in memory. Users could not know how
    much memory is used for an object, like in C.
    However this tricky method lets them to know in an
    indirect way.
    To achieve how much memory is used for an object we must
    calculate the used memory before and after the object is
    created.
    MemoryUsage.java
    * User: Pinkman - Fuat Geleri
    * Date: Mar 20, 2005
    * Time: 11:13:50 AM
    * The class which makes the calculation job.
    * Calculating the difference of used memory
    * between calls
    * "start()", and "end()".
    * So if you initiate an object between those
    * calls you can get how much memory the object
    * consumed.
    * Initial inspration from
    * http://javaspecialists.co.za/archive/Issue029.html
    public class MemoryUsage {
         long usage;
         public void start(){
              garbageCollect();
              Runtime r=Runtime.getRuntime();
              usage=r.totalMemory()-r.freeMemory();
         public long end(){
              garbageCollect();
              Runtime r=Runtime.getRuntime();
              return (r.totalMemory()-r.freeMemory())-usage;
         public String memorySizeToString(long l){
              int MB=(int) (l/1048576);
              l=l-1048576*MB;
              int KB=(int) (l/1024);
              l=l-1024*KB;
              return new String(MB+"MB "+KB+"KB "+l+"BYTES");
         private void garbageCollect() {
              Runtime r=Runtime.getRuntime();
              r.gc();r.gc();r.gc();r.gc();r.gc();
              r.gc();r.gc();r.gc();r.gc();r.gc();
              r.gc();r.gc();r.gc();r.gc();r.gc();
    Therefore the first file MemoryUsage.java is coded.
    It simply calculates and stores the used memory when
    start() method is called. After generating some objects,
    the end() method is called to get the
    difference between memory usages, between the start()
    and end() method calls.
    SizeOf.java
    import javax.swing.tree.DefaultMutableTreeNode;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Random;
    * User: Pinkman - Fuat Geleri
    * Date: Mar 20, 2005
    * Time: 6:02:54 PM
    * Arbitrarily size of objects in JAVA..
    * An example of getting how much an
    * object consumed in memory.
    * Like
    *  primitives -> does not consume any
    *  arrays  -> consumes 16 bytes when empty
    *   ex: byte []a=new byte[0];
    *  list    -> consumes 80 byte when empty..
    *  references -> when did not initiated does
    * not consume any memory!.
    * Initial inspration from
    * http://javaspecialists.co.za/archive/Issue029.html
    public class SizeOf {
         MemoryUsage mu=new MemoryUsage();
         public static void main(String []args){
            SizeOf s=new SizeOf();
              s.sizeOfExample();
         //remove the comments in order to make the same checking by yourself..
         private void sizeOfExample() {
              mu.start();
              //byte []b=new byte[0];//<-byte array 16byte,each byte addition equals 1 byte!!
              //String s="How much memory is used?";//string consumes no byte!!
              //byte c=20; //<-consumes no memory!!
                    //Object o=new Object();//object consumes 8 bytes..
              //Object []oa=new Object[100];//<-object array consumes 16 byte! and each addition object 4 bytes!
                    //List list;//non initialized object consumes no memory!!..
              //Integer i=new Integer(1);//an integer object consumes 16 bytes!
              //List list=new ArrayList();//An array list consumes 80 bytes!.
              /*for(int i=0;i<10;i++){ //An array list + 10 integer consumes 240 bytes  :)
                   list.add(new Integer(i));
              Random r=new Random();
              byte []rand=new byte[1];
              int count=100000;
              List list=new ArrayList(count);
              for(int i=0;i<count;i++){
                   r.nextBytes(rand);
                   list.add(new String(rand));//empty string occupies no memory??
              DefaultMutableTreeNode root=new DefaultMutableTreeNode();//8 byte when single!.
              Random r=new Random();
              byte []rand=new byte[10];//when this is one and count is 1 million memory gets overfilled!..
              int count=500000;
              for(int i=0;i<count;i++){
                   r.nextBytes(rand);
                   root.add(new DefaultMutableTreeNode(new String(rand)));
              long l=mu.end();
              System.out.println(""+mu.memorySizeToString(l));
    An example usage can be found in the second file
    SizeOf.java. Simply remove the comments,"//", and execute
    to see how much memory used.
    For example:
    % Primitives does not consume any memory.
    % Non-initialized object references does not consume
    any memory.
    % Empty string and most of the small strings does not consume any memory.
    % Empty byte[] consumes 16 bytes.
    % Empty ArrayList consume 80 bytes.
    % An Integer object consumes 16 bytes.
    % Empty DefaultMutableTreeNode consumes 8 bytes..
    and so on.
    You can find the mentioned files in the attachments.
    I heard the idea of -how much memory is used- from
    Prof. Akif Eyler, and i got the main inspration from
    the site http://javaspecialists.co.za/archive/Issue029.html
    //sorry about my mistakes, and everything :)
    Thanks for your answers beforehand.

    Any good profiler will tell you.
    You can also code your own using jvmpi or jvmti.
    Also note that calling System.gc is a hint to the jvm it may or may not respect your wish.
    Test it with a few different gc algorithms and see what happens.
    /robo

  • Acmcneill1ug 14, 2014 7:16 AM I have IMac OSX 10.9.4, 4GB,Processor 3.06 with Intell2Duo. I would like to check for Malware. I run a TechTool Pro 6 every month and that comes up great. When check how much memory I am using, with only Safar

    Acmcneill1ug 14, 2014 7:16 AM
    I have IMac OSX 10.9.4, 4GB,Processor 3.06 with Intell2Duo. I would like to check for Malware. I run a TechTool Pro 6 every month and that comes up great.
    When check how much memory I am using, with only Safari open I am using 3.9 and more of her 4.0 memory. She is very. very slow in processing. I had 4000
    trash to clean out and it took her over an hour to expel. Also for some reason Safari will not allow me to click on a link, in my G-mail, and let it go to the page.
    It has a sign saying a pop-up blocker is on and will not let me do it. I must open the stamp to look at my e-mails and if I have redirected link now I can do it.
    I have not changed my preferences so where is this pop-up blocker?
    I have looked at preferences on Safari and Google, which I do not understand Google, and do not see where this blocker could be.
    Malware is something I want to make sure is not on my computer. Tech Tool Pro 6 is all I know of and it does not detect Malware.
    Help.
    Ceil

    Try Thomas Reed's Adware removal tool. He posts extensively in the communities.
    Malware Guide - Adware
    Malware Discussion

  • When it says your capacity is 6.43 GB does that mean how much memory you have used up or how much you have left

    when it says your capacity is 6.43 GB does that mean how much memory you have used up or how much you have left

    The 6.43 GB is how much storage that is available to you to use. The difference between 6.43 ansd 8 GB is taken up by the iOS.
    The Available entry show how much is unused.

  • How do I see how much memory is being used in 10.6.8

    I keep getting an error message that tells me I do not have enough memory.  And all of a sudden most of my disk space is gone. It went from having hundreds of free space to 90. gb.  I don't know how to check which programs are using all the memory and I have no big files down loaded.  I have an older imac, but it has always served my needs.  I have thrown away photos, (extras) Dumped some music, but It just does not help.  I have also restarted and repaired disk permissions. I can't afford a new computer right now.  HE

    Disk Space - Free Up
    Disk Space – Free Up (2)
    Disk Space Filling Up – OmniDiskSweeper
    Disk Space Filling Up - WhatSize

  • HT1635 how much memory I can use w my macbook late 2007

    to whom it may concern:
    How I upgrade my memory of macbook 2007,  I have 2gb

    Get the user manual: MacBook (13-inch, Mid 2007) - User Guide. If you have the Mid-2007 model you can install 4 GBs (2 GBs in each slot) but only 3 GBs will be active. If you have a Late-2007 model you can install up to 6 GBs of RAM.

  • How much memory can Premiere Pro use on a new Mac Pro?

    I'm ready to upgrade memory on my Mac Pro Late 2013 6-core.  Does PP CC 2014 have a limit on how much RAM it can use?  Some apps never use more than a certain amount, so I don't want to buy a lot of memory if PP will never put it to work.  (I made that mistake when I was an FCP user.)  Is there a limit and if so, what?
    Also, are there any Mac Pro users who have an opinion on optimum RAM?  I currently have 16GB (4x4GB) with 10GB reserved for PP.  I am in the middle of a very large project and have noticed some irregularities and spinning balls.  I assume that they are from memory shortage since quitting all other apps and restarting PP seems to fix the issue.

    Not a Mac expert here ... but I do know there's folks running 64G ram and so going up from 16 will work fine. Now ... as to which is the "choke-point" ... that's a bit more techie than I can answer off the bat.
    The Adobe video apps can put a pretty good strain on all of the computer's hardware, and "balancing" the assets of the hardware to the usage patterns of the software is always the trick. Memory never hurts ... but if the spinning ball is a video-ram problem, changing mobo ram isn't going to do much. The various areas they go into great detail on within the pages of the "Tweakers Page" (found within these forums threads, check the hardware forum) are of course, mobo, chip/s & cores, ram, disc in-outs & arrays/layout of project/footage & program files, video card & ram.
    Your problem might be best solved by more RAM; it might be that it's a disc read/write issue and either more internal or eSATA II discs to spread read/write processes over would be best ... or taking an array of discs and using a fast multi-disk RAID array to speed things up even faster, or a video card upgrade.
    So ... where you might want to go is the the Hardware Forum and give your computers complete specs, including disc layout and usage, and see what folks there think needs tweaking.
    Neil

  • How much memory are actually being used? db_32k_cache_size

    Hi everyone,
    Using Oracle 10g on RHEL 5.3.
    I've been asked to check how much memory are actually being used by our db cache's. Currently we are using the parameters db_32k_cache_size (user-defined) and db_8k_cache_size (default) both allocated with 12gb. Now I want to know if there is any way to find out how much of the 12gb are actually being used by the cache.
    Is this possible?
    Thanks

    Thanks Guys,
    Okay, so it is using the whole 12gb. I guess the next question would be, is 12gb really needed? You see, we are trying to increase memory where possible as this database has performance issues. Now we are thinking of taking some of the 12gb that is allocated to the 32k cache and assign it to the sga for instance.
    Does this make any sense?
    Regards

  • HT3939 How can I find out how much memory is used to run the iphone 5c and the 5s ?

    How or where can i find out how much memory the OS  iphone 5c and installed apps. use?  Then to find out how much memory the apps i plan to use uses? THANK YOU ALL. I HAVE A PURRING KITTEN IN MY ARMS.

    Hello comodotomi
    If the app is in iTunes, you can get information on what Apple ID was associated with the purchase. The steps to do this are outline in the article below.
    Recovering a forgotten iTunes Store account name
    http://support.apple.com/kb/ht1920
    Thanks for using Apple Support Communities.
    Regards,
    -Norm G. 

  • Apps are using too much memory. how do i find out how much each app is using?

    MACAir Memory 128gb: the apps are using too much memory. i get not sufficient memory for upgrades. how can i find out how much memory each app is using?

    Mavericks is designed to use RAM differently for good reason. Normal
    http://appleinsider.com/articles/13/06/12/compressed-memory-in-os-x-109-maverick s-aims-to-free-ram-extend-battery-life
    Mavericks uses RAM much more efficiently than any other previous version of OS X. Mavericks now has a memory compression feature that will compress the memory occupied by applications that aren't being actively used and give the freed up RAM to the application that needs it the most.
    Memory Pressure graph that tells you how stressed is the system in terms of memory.
    Fore more information about OS X Mavericks new system management technologies visit:
    http://www.apple.com/osx/advanced-technologies/
    See page 5 here:
    http://images.apple.com/media/us/osx/2013/docs/OSX_Mavericks_Core_Technology_Ove rview.pdf

  • I have an iMac 5,1 with an Intel Core 2 Duo. I'd like to be able to use iCloud, but need Lion which is too big. How much memory can I add? And will this upgrade allow me to install Lion?

    I have an iMac 5,1 with an Intel Core 2 Duo. I'd like to Lion, in order to use iCloud, but the specs indicate I don't have enough memory. How much memory can I add? And will this upgrade allow me to install Lion?

    I believe your model iMac can address 3 GB RAM; the minimum for Lion is 2 GB - however, that is an absolute minimum for very light use. You would be much happier with a minimum of 4 GB.
    http://www.everymac.com/systems/apple/imac/stats/imac-core-2-duo-2.16-20-inch-sp ecs.html
    (You can install 4 GB, but it will only be able to address about 3.1 - 3.2 GB)

  • Can each Thread say how much memory it wants to use ?

    Can each Thread say how much memory it wants to use ?
    or any other way to identify this ?
    namanc

    No

  • How much memory does Tiger and Leopold use?

    Hi
    How much memory do both Tiger and Leopold require themselves to run?

    hotcheese wrote:
    How much memory do both Tiger and Leopold require themselves to run?
    Not much. I've run Tiger and Leopard with only 512 MB (1 GB machine with
    512 MB for OS-X and 512 MB allocated to a virtual machine running Win XP).
    They get really slow with a lot of page-outs if you open more than one app,
    but they do run. The standard 1 GB (with no VMs) is comfortable for web
    browsing and even iPhoto -- as long as you don't open too many apps at
    the same time.
    ...don't know 'bout Leopold, I think he's still dead,
    Looby

  • How much memory for the G5 2.7?

    I am buying a refurbished G5 2.7 from Apple.
    The primary application will be Pro Tools HD2 for audio mixing.
    QUESTIONS:
    (1) How much memory will I likely need? 2GB? 4GB?
    (2) I will buy the memory from Crucial. Should I remove the original memory and use only the Crucial 1GB pairs, so they are matched?
    Powermac G5 2.7   Mac OS X (10.4.6)  

    No need to remove the original RAM.
    How much to install depends on how you'll be using ProTools. If it's only audio (no sample players), 2GB would probably work perfectly. If you're using samplers heavily, with lots of large samples, you might want to go to 5-6GB.
    I'd start with 4GB, RAM for these is cheap (compared to RAM for the Mac Pro.)

  • How do I find out how much memory can my macbook upgraded?

    How do I find out how much memory can my macbook upgraded?

    You can use either Everymac http://www.everymac.com/ or the freeware MacTracker http://www.mactracker.ca/
    to get the info if you don't want to share the specs of your MacBook here.
    Regards
    Stefan

Maybe you are looking for

  • When opening a folder, its takes ages to show the files

    Hi All, I have a 2012 Macbook Pro running Lion (as you can probably guess!), with a 512Gb SSD and 8Gb Ram. Its a pretty fast machine and i'm really happy with the way it runs, however when I open a folder to view files, it opens and shows a blank fol

  • Query re creating a new due date schedule

    Hi We are looking to create a new suite of 'free-choice' courses whereby students can choose their own selection of modules at the start of each of three semesters and pay for them, in total, at that time. As the fee will be based around the course,

  • Adding Zeros to Blank Cells

    Hi, I am looking at what should be a simple piece of config, but I am struggling to implement. I want to show Zeros in the blank cells in my reports, in order that I can apply conditions and exceptions to them. Can anyone suggest a simple solution, a

  • Oracle CPU question with 11.5.10 CU2

    Just a general question. First time trying to apply an Oracle CPU and in this case applying the July 2010 CPU to our Test E-biz 11.5.10 CU2 RUP7 install. My configuration is RH Linux ES 4.8 with a 10.2.0.4 database server running admin, db and CCM se

  • Problem installing CS 5.5 Design Premiun InDesign on new computer

    In 2011 I successfully installed CS 5.5 Deisgn Premium on my Windows XP machine. Recently I replaced that system with a new Windows 7 machine and have again successfully installed CS 5.5 Design Premiun - with the exception of InDesign. My first insta