overcoming a dealocating memory problem ?

Discussion in 'Java' started by Jole, May 12, 2004.

  1. Jole

    Jole Guest

    Hi
    I'm using serialization to read in an object from a file. My code basically
    does this:

    1)MyClass obj = (MyClass) in.readObject();

    ('in' is an ObjectInputStream wrapped around a FileInputStream object, ie
    the code reads an object stored in a file on disk)

    Now, what happens is my code does this once at the start (works fine), but
    later i need to do the same thing, to read in an updated version of the
    object. ie, i need to do this again:

    2) obj = (MyClass) in.readObject();


    the compiler tells me that obj is constant and can't be assigned again. I
    was hoping i could do something like this at step 2:

    delete obj; //free memory
    obj = (MyClass) in.readObject(); //read in new version of object from file
    //and assign it to the same reference

    (don't pay attention to the syntax, but the concepts..ie the above frees the
    associated memory and then reads in a new copy from the file and assigns it
    to the reference obj)

    There doesn't seem to be a way to do it. Any ideas?

    thanks
    Jole
     
    Jole, May 12, 2004
    #1
    1. Advertising

  2. On Wed, 12 May 2004 08:48:21 GMT, Jole wrote:
    > I'm using serialization to read in an object from a file. My code
    > basically does this:
    >
    > 1)MyClass obj = (MyClass) in.readObject();


    [...]

    > 2) obj = (MyClass) in.readObject();


    > the compiler tells me that obj is constant and can't be assigned
    > again.


    How *exactly* did you declare obj? Did you use "final"? Don't!

    In general, you'll get better help if you post real code, instead of
    what your code "basically" does, since in most cases whatever is
    causing the error doesn't get posted when you summarize like that.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 12, 2004
    #2
    1. Advertising

  3. Jole wrote:
    > 2) obj = (MyClass) in.readObject();
    >
    >
    > the compiler tells me that obj is constant and can't be assigned again. I
    > was hoping i could do something like this at step 2:
    >
    > delete obj; //free memory
    > obj = (MyClass) in.readObject(); //read in new version of object from file
    > //and assign it to the same reference


    Your problem has nothing to do with allocation of memory. The only case where the
    compiler wouldn't allow you assign to the variable again is if it's declared as
    "final". Remove that keyword from the variable declaration and it should work.

    You seem to be completely unfamiliar with the way memory management in Java works.
    It uses "garbage collection". Memory is allocated when an object is created via
    "new" (or cloned, or deserialized) and deallocated automatically when there are
    no more references to it.

    You have a potential problem in your code because ObjectInputStream retains
    references to all objects read from it, so their memory can't be reclaimed
    as long as the stream is not closed or reset.
     
    Michael Borgwardt, May 12, 2004
    #3
  4. Jole

    Tony Morris Guest

    "Jole" <> wrote in message
    news:pxloc.34308$...
    > Hi
    > I'm using serialization to read in an object from a file. My code

    basically
    > does this:
    >
    > 1)MyClass obj = (MyClass) in.readObject();
    >
    > ('in' is an ObjectInputStream wrapped around a FileInputStream object, ie
    > the code reads an object stored in a file on disk)
    >
    > Now, what happens is my code does this once at the start (works fine), but
    > later i need to do the same thing, to read in an updated version of the
    > object. ie, i need to do this again:
    >
    > 2) obj = (MyClass) in.readObject();
    >
    >
    > the compiler tells me that obj is constant and can't be assigned again. I
    > was hoping i could do something like this at step 2:
    >
    > delete obj; //free memory
    > obj = (MyClass) in.readObject(); //read in new version of object from

    file
    > //and assign it to the same reference
    >
    > (don't pay attention to the syntax, but the concepts..ie the above frees

    the
    > associated memory and then reads in a new copy from the file and assigns

    it
    > to the reference obj)
    >
    > There doesn't seem to be a way to do it. Any ideas?


    Since you clearly lack fundamental understanding of the technology that you
    are using (Java), two questions arise:

    a) ummm... what ? You have provided a problem statement that has been
    constructed in an ad hoc fashion, which is erroneous and thus, contains no
    valuable description of your actual problem. One can only speculate at what
    your actual problem (the best guess at the moment is the use of the modifier
    'final'). Sometimes "you don't know what you don't know"; that is, you'd
    describe it better if you could, but due to a lack of understanding, you
    can't. In this case, it is best to provide a working code sample, that
    compiles (so that more experienced people can reproduce your problem), and
    also contains your expected output and the actual output. That way, your
    problem is communicated clearly (by the code), and so is your
    misunderstanding (your expected outcome).

    b) Why are you using a technology in such a way (i.e. Serialization) that
    requires at least a fundamental understanding the concepts involved ?
    Sometimes it is best to just drop your pride, accept your shortcomings and
    start back at the beginning.
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
     
    Tony Morris, May 12, 2004
    #4
  5. Jole

    Jole Guest

    Hi all,
    Sorry if i've upset everyone. I'm still looking at this problem, but i may
    have the answer. That code i wrote was a summary of it.

    here is the real:

    //Get_New() member function of class MyClass
    File ListFile2 = new File("list2");
    FileInputStream fis2 = new FileInputStream(ListFile2);
    ObjectInputStream inn = new ObjectInputStream(fis2);
    this = (MyClass) inn.readObject();
    inn.close();
    //end member function

    the compiler says:
    cannot assign a value to final variable this..

    Okay, i know what yous are going to say, 'it's dead easy, and my code is
    different from my previous summary code'. yes, but b4 my summary code
    produced the same error. Although now it doesn't . I'm still looking into
    why this is the case, as it didn't work b4. Anyhow, it seems that you
    can't use 'this' to make the object point to another one newly created in
    memory. But you have to use this normal syntax form only:

    old_object_name = (MyClass) inn.readObject();


    anyway, thanks again.
    J





    Jole wrote:

    > Hi
    > I'm using serialization to read in an object from a file. My code
    > basically does this:
    >
    > 1)MyClass obj = (MyClass) in.readObject();
    >
    > ('in' is an ObjectInputStream wrapped around a FileInputStream object, ie
    > the code reads an object stored in a file on disk)
    >
    > Now, what happens is my code does this once at the start (works fine), but
    > later i need to do the same thing, to read in an updated version of the
    > object. ie, i need to do this again:
    >
    > 2) obj = (MyClass) in.readObject();
    >
    >
    > the compiler tells me that obj is constant and can't be assigned again. I
    > was hoping i could do something like this at step 2:
    >
    > delete obj; //free memory
    > obj = (MyClass) in.readObject(); //read in new version of object from
    > file //and assign it to the same reference
    >
    > (don't pay attention to the syntax, but the concepts..ie the above frees
    > the associated memory and then reads in a new copy from the file and
    > assigns it to the reference obj)
    >
    > There doesn't seem to be a way to do it. Any ideas?
    >
    > thanks
    > Jole
     
    Jole, May 12, 2004
    #5
  6. Jole wrote:

    > here is the real:
    >
    > //Get_New() member function of class MyClass
    > File ListFile2 = new File("list2");
    > FileInputStream fis2 = new FileInputStream(ListFile2);
    > ObjectInputStream inn = new ObjectInputStream(fis2);
    > this = (MyClass) inn.readObject();
    > inn.close();
    > //end member function
    >
    > the compiler says:
    > cannot assign a value to final variable this..


    And the compiler is exactly right. The compiler message may be a bit
    confusing because "this" is not really a variable at all, but the
    compiler is quite right that you can never assign to "this". (The
    "this" variable refers to the object on which the current method is
    operating -- it doesn't even make sense to try to assign to it.)

    > Okay, i know what yous are going to say, 'it's dead easy, and my code is
    > different from my previous summary code'. yes, but b4 my summary code
    > produced the same error.


    Which then told you exactly what the problem was ("cannot assign to
    final variable <foo>"), just as several people surmised from your vague
    description.

    > Although now it doesn't .


    On yet again different code, apparently.

    > I'm still looking into
    > why this is the case, as it didn't work b4. Anyhow, it seems that you
    > can't use 'this' to make the object point to another one newly created in
    > memory.


    Answer one or more of the following:
    (1) What do you even mean by that?
    (2) Why would you think there was any chance of it working?
    (3) If it somehow did work, what would the result be?

    > But you have to use this normal syntax form only:
    >
    > old_object_name = (MyClass) inn.readObject();


    () That is the only syntax available for assigning object references to
    variables.
    () Variables are the only program entities that can be assigned to.
    () Final variables can only be assigned to once, and must provably be
    assigned before their value is ever used.
    () "this" is not a variable at all, but if it were one it would
    certainly be final.

    Go learn Java if you plan to continue using it.


    John Bollinger



    P.S.: comp.lang.java does not exist. Do not post there, and especially
    do not direct followups there. When you do direct followups to a
    different group, advise that you are doing so. -- JCB
     
    John C. Bollinger, May 12, 2004
    #6
  7. Jole

    Jole Guest

    John C. Bollinger wrote:

    > Jole wrote:
    >
    >> here is the real:
    >>
    >> //Get_New() member function of class MyClass
    >> File ListFile2 = new File("list2");
    >> FileInputStream fis2 = new FileInputStream(ListFile2);
    >> ObjectInputStream inn = new ObjectInputStream(fis2);
    >> this = (MyClass) inn.readObject();
    >> inn.close();
    >> //end member function
    >>
    >> the compiler says:
    >> cannot assign a value to final variable this..

    >
    > And the compiler is exactly right. The compiler message may be a bit
    > confusing because "this" is not really a variable at all, but the
    > compiler is quite right that you can never assign to "this". (The
    > "this" variable refers to the object on which the current method is
    > operating -- it doesn't even make sense to try to assign to it.)
    >
    >> Okay, i know what yous are going to say, 'it's dead easy, and my code is
    >> different from my previous summary code'. yes, but b4 my summary code
    >> produced the same error.

    >
    > Which then told you exactly what the problem was ("cannot assign to
    > final variable <foo>"), just as several people surmised from your vague
    > description.
    >
    >> Although now it doesn't .

    >
    > On yet again different code, apparently.
    >
    >> I'm still looking
    >> into
    >> why this is the case, as it didn't work b4. Anyhow, it seems that you
    >> can't use 'this' to make the object point to another one newly created in
    >> memory.

    >
    > Answer one or more of the following:
    > (1) What do you even mean by that?
    > (2) Why would you think there was any chance of it working?
    > (3) If it somehow did work, what would the result be?



    hi..what i was trying to do was to update that object through a member
    function. so instead of doing:
    object = (MyClass) inn.readObject();
    i wanted a MyClass.Get_New() member function to do it. So i thought of
    using 'this' (as it's a reference to the object in memory), and assigning
    it to another newer copy read from a file. Anyway, turns out 'this' is
    constant so you can't do it. I'll have to use normal object references
    instead.



    >
    >> But you have to use this normal syntax form only:
    >>
    >> old_object_name = (MyClass) inn.readObject();

    >
    > () That is the only syntax available for assigning object references to
    > variables.
    > () Variables are the only program entities that can be assigned to.
    > () Final variables can only be assigned to once, and must provably be
    > assigned before their value is ever used.
    > () "this" is not a variable at all, but if it were one it would
    > certainly be final.
    >
    > Go learn Java if you plan to continue using it.
    >
    >
    > John Bollinger
    >
    >
    >
    > P.S.: comp.lang.java does not exist. Do not post there, and especially
    > do not direct followups there. When you do direct followups to a
    > different group, advise that you are doing so. -- JCB
     
    Jole, May 13, 2004
    #7
  8. Jole wrote:
    > hi..what i was trying to do was to update that object through a member
    > function. so instead of doing:
    > object = (MyClass) inn.readObject();
    > i wanted a MyClass.Get_New() member function to do it. So i thought of
    > using 'this' (as it's a reference to the object in memory), and assigning
    > it to another newer copy read from a file. Anyway, turns out 'this' is
    > constant so you can't do it. I'll have to use normal object references
    > instead.


    You might be able to get the same results by replacing all the fields of
    the object with those of the new one.
     
    Michael Borgwardt, May 13, 2004
    #8
    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. Ram CBE
    Replies:
    0
    Views:
    341
    Ram CBE
    Aug 9, 2003
  2. kj
    Replies:
    4
    Views:
    400
    Paul Du Bois
    Jun 21, 2005
  3. Hughes, Chad O
    Replies:
    6
    Views:
    366
    Dennis Lee Bieber
    Jun 21, 2005
  4. Jack Daynes

    Overcoming POSTDATA Warning

    Jack Daynes, Jan 18, 2007, in forum: Java
    Replies:
    1
    Views:
    388
    Daniel Pitts
    Jan 18, 2007
  5. none
    Replies:
    4
    Views:
    363
    Abhishek Padmanabh
    Mar 27, 2010
Loading...

Share This Page