Re: overcoming a dealocating memory problem ?

From: Michael Borgwardt (brazil_at_brazils-animeland.de)
Date: 05/12/04


Date: Wed, 12 May 2004 11:18:51 +0200

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.



Relevant Pages

  • Re: Readings on clr optimization?
    ... The compiler cannot inline a call to a constructor if the class has a static ... Weak references are special objects that allow you to track references that ... There aren't many implication with memory ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Mixed mode __pin internals: does __pin does anything at all?
    ... Remember that the code is JIT compiled, the compiler may generate different ... code for pinned references than unpinned references. ... prevent Windows from moving the memory containing the buffers to the swap ... compact the heap to reduce the number of actual memory pages used. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: overcoming a dealocating memory problem ?
    ... > the compiler tells me that obj is constant and can't be assigned again. ... 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. ... references to all objects read from it, so their memory can't be reclaimed ...
    (comp.lang.java)
  • Re: WWDC -- MacBook Pro?
    ... ....but once the program has loaded it into its program memory, ... boundaries, int16 are aligned on 2 byte boundaries, int32 are ... people are always able to come up with the compiler ... Case B, same glass, same ice code water.  ...
    (comp.sys.mac.system)
  • Re: Javas performance far better that optimized C++
    ... The compiler is extremely stupid. ... no memory leaks are guaranteed. ... However I have GC in my .NET programming. ... "C.9.1 Automatic Garbage Collection ...
    (comp.lang.cpp)