about static method

Discussion in 'Java' started by Jeff Zhao, Aug 18, 2005.

  1. Jeff Zhao

    Jeff Zhao Guest

    Hi everyone,

    I know static fields are always hosting in memory.But,how about
    static methods? Are they always hosting in memory? I think they
    aren't,but how are they running?

    thanks,

    Best rgds.
     
    Jeff Zhao, Aug 18, 2005
    #1
    1. Advertising

  2. Jeff Zhao wrote:
    > I know static fields are always hosting in memory.But,how about
    > static methods? Are they always hosting in memory? I think they
    > aren't,but how are they running?


    All instance fields of every object in the JVM and all static fields of
    every class in the JVM are always in (possibly virtual) memory. Some
    version of the code for all methods, static and non-static, of all
    classes currently loaded in the JVM are always in (possibly virtual)
    memory. These are direct implications of an instance being live in a
    JVM and of a class being loaded in a JVM. Why do you suggest otherwise?

    Methods do not run in any case. Threads run, executing methods' code in
    the process.

    --
    John Bollinger
     
    John C. Bollinger, Aug 18, 2005
    #2
    1. Advertising

  3. Jeff Zhao

    Roedy Green Guest

    On 17 Aug 2005 20:21:13 -0700, "Jeff Zhao" <> wrote
    or quoted :

    > I know static fields are always hosting in memory.But,how about
    >static methods? Are they always hosting in memory? I think they
    >aren't,but how are they running?


    They hang around in RAM until there are no more references to them.
    Then they can be GCed like anything else.
     
    Roedy Green, Aug 18, 2005
    #3
  4. Jeff Zhao

    jan V Guest

    > > I know static fields are always hosting in memory.But,how about
    > >static methods? Are they always hosting in memory? I think they
    > >aren't,but how are they running?

    >
    > They hang around in RAM until there are no more references to them.
    > Then they can be GCed like anything else.


    Static *methods* ?? Surely not. And what's a reference to a static method?
     
    jan V, Aug 18, 2005
    #4
  5. Jeff Zhao

    Jeff Zhao Guest

    John C. Bollinger 写é“:

    > Jeff Zhao wrote:
    > > I know static fields are always hosting in memory.But,how about
    > > static methods? Are they always hosting in memory? I think they
    > > aren't,but how are they running?

    >
    > All instance fields of every object in the JVM and all static fields of
    > every class in the JVM are always in (possibly virtual) memory. Some
    > version of the code for all methods, static and non-static, of all
    > classes currently loaded in the JVM are always in (possibly virtual)
    > memory. These are direct implications of an instance being live in a
    > JVM and of a class being loaded in a JVM. Why do you suggest otherwise?
    >
    > Methods do not run in any case. Threads run, executing methods' code in
    > the process.
    >
    > --
    > John Bollinger
    >



    then,does it work like following?

    1. when class isn't be loaded in JVM
    --all fields not in memory

    2. when class is loaded in JVM
    --static fields are in memory,but instance fields aren't in
    memory,all methods not in memory

    3. when class is loaded in JVM,but it isn't be initialized,and its
    static method is invoked
    --all fields are in memory,static methods in memory,but instance
    methods aren't in memory

    4. when class is initialized
    --all fields are in memory,all methods are in memory

    5. Whether it's true or not? and at last,I want to know when are they
    be GCed?
    I'm puzzled by Roedy,static fields can be GCed? And static methods
    too?
     
    Jeff Zhao, Aug 18, 2005
    #5
  6. Jeff Zhao

    Chris Uppal Guest

    Jeff Zhao wrote:

    > then,does it work like following?
    >
    > 1.[...]


    No. The following is a simplification (there are exceptions in certain
    "advanced" situation -- all of which involve classloaders), but it's close
    enough for a working approximation:

    When a class is loaded, the code for its instance methods
    and static methods are treated identically. They are copied
    into the JVM's memory and stay there (with the class itself)
    until the JVM exits. Nothing is ever GCed.

    Instance fields are part of the instances, so the memory used
    for storing any instance's fields is allocated when the instance
    is created, and released when the instance is GCed. Static
    fields are part of the class, and their memory is allocated when
    the class is loaded, and never released until the JVM exits.
    (Or until the class is itself GCed -- but that can never happen
    except in special applications which make sophisticated use
    of classloaders)

    (Actually, the JVM is at liberty to play tricks to reduce memory consumption,
    as long as that is never visible to application programmers. That's an
    implementation detail, and will vary from JVM to JVM. As far as I, personally,
    know the only "trick" that is used is that the JITed form of the methods' code
    /can/ be created lazily (when the method is used, or is used more than a
    certain number of times), and that the compiled machine-code representation
    /can/ be thrown away if it has not been used for a while.)


    > I'm puzzled by Roedy,static fields can be GCed? And static methods
    > too?


    Roedy is wrong. If he is confusing you then it's probably best just to ignore
    him (on this subject).

    -- chris
     
    Chris Uppal, Aug 18, 2005
    #6
  7. Jeff Zhao

    Roedy Green Guest

    On 18 Aug 2005 00:27:46 -0700, "Jeff Zhao" <> wrote
    or quoted :

    >1. when class isn't be loaded in JVM
    > --all fields not in memory
    >
    >2. when class is loaded in JVM
    > --static fields are in memory,but instance fields aren't in
    >memory,all methods not in memory


    Instance fields only exist in objects. So until you instantiate some
    objects of the class, this is exactly how it works.
     
    Roedy Green, Aug 19, 2005
    #7
  8. Jeff Zhao

    Roedy Green Guest

    On 18 Aug 2005 00:27:46 -0700, "Jeff Zhao" <> wrote
    or quoted :

    > --all fields are in memory,static methods in memory,but instance
    >methods aren't in memory


    Sometimes the methods are loaded one at a time as they are used,
    static or instance. It is really up to the JVM to do it how it sees
    fit. The way JVMs are designed is based mainly on space/time
    tradeoffs. It is obviously faster to load an entire class as a lump
    and instantiate all its methods than to do it one at a time.

    Keep in mind that there is only one copy of the code for a method in
    RAM, whether it is static or instance. Instance methods are only
    logically connected to objects, not duplicated.
     
    Roedy Green, Aug 19, 2005
    #8
  9. Jeff Zhao

    Roedy Green Guest

    On 18 Aug 2005 00:27:46 -0700, "Jeff Zhao" <> wrote
    or quoted :

    > I'm puzzled by Roedy,static fields can be GCed? And static methods
    >too?


    Inside the JVM there is an object for each class that will point to
    the code for its various static and instance methods and its static
    variables, and also its superclass. This thing is an object like any
    other, even if you don't play with it directly, and is subject to the
    same GC rules as any other object.

    However, I can't think of a time where I noticed a class
    reinitialising its statics in mid run after a period of disuse. So I
    suspect in practice classes don't often get GCed. You might be able
    to find out with the java -verbose option that logs every class
    loaded. You could see if some got loaded twice, implying a GC.
     
    Roedy Green, Aug 19, 2005
    #9
  10. Jeff Zhao

    Roedy Green Guest

    On Thu, 18 Aug 2005 07:03:43 GMT, "jan V" <> wrote or quoted
    :

    >Static *methods* ?? Surely not. And what's a reference to a static method?


    A piece of code that invokes a static method would be a reference to a
    static method, but GC is only interested in what class references
    point to, not which method within that class.

    On the other hand, a method might be inlined, interpreted or compiled.
    Over a session it might even exist in all three forms. Each method of
    the class can be handled separately. Presumably GC scavenges
    discarded compilations whose "effectively final" assumptions have been
    ruined by a new class loaded.
     
    Roedy Green, Aug 19, 2005
    #10
  11. Jeff Zhao

    Roedy Green Guest

    On Thu, 18 Aug 2005 09:29:31 +0100, "Chris Uppal"
    <-THIS.org> wrote or quoted :

    > When a class is loaded, the code for its instance methods
    > and static methods are treated identically. They are copied
    > into the JVM's memory and stay there (with the class itself)
    > until the JVM exits. Nothing is ever GCed.


    Is this part of the JVM spec or just the way some particular JVM
    works? Where did you read this? Unfortunately I can't recall where I
    read the story I have been repeating.
     
    Roedy Green, Aug 19, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,648
    cgbusch
    Jul 5, 2003
  2. Gary
    Replies:
    1
    Views:
    3,991
    Ryan Stewart
    Jan 16, 2004
  3. Michael
    Replies:
    3
    Views:
    4,944
    Adam Jenkins
    Jan 27, 2004
  4. G. Burton
    Replies:
    2
    Views:
    59,899
    Sudsy
    Feb 21, 2004
  5. Ryan
    Replies:
    3
    Views:
    19,556
    dingo
    Nov 17, 2004
Loading...

Share This Page