By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

overcoming a dealocating memory problem ?

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 17 '05 #2

P: n/a
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.
Jul 17 '05 #3

P: n/a
"Jole" <no*****@nospam.com> wrote in message
news:px******************@news-server.bigpond.net.au...
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/tutor...lts/index.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)

Jul 17 '05 #4

P: n/a
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


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.