Re: Java Memory question
On Thu, 17 Mar 2011 08:38:51 -0700 (PDT), Eric
<e.d.programmer@gmail.com> wrote:
On Mar 16, 9:34??pm, rossum <rossu...@coldmail.com> wrote:
On Wed, 16 Mar 2011 11:35:43 -0700 (PDT), Eric
<e.d.program...@gmail.com> wrote:
The actual discussion was how to reduce the memory usage of a class
instance object if anyone has any tips.
You have already been told about some tings you could try.
Is the memory error on the client or on the server? ??Have a look at
the appropriate code, and run a profiler on teh appropriate machine,
to see where the memory hog is.
Either you do not have enough memory to do what you want or you have
something that is eating you memory.
From what you have said so far, I would not be surprised if you are
packratting, which is a good way to waste memory.
rossum
That was the first concern mentioned here, to limit scope. If I have
2 methods which need an object of the same type but don't share the
value, it uses fewer lines of code to declare one variable in the
class to be used by the instance then reuse that variable for each
method to create it's object which should also reserve a space in
memory for that object and seems like it should run faster, but
Number of lines of code are not relevant to memory use. Here is a
single line of code:
Object[] myBigThing = new Object[Integer.MAX_VALUE];
Now put that object into your class:
class MyMemoryHogClass {
private Object[] myBigThing = new Object[Integer.MAX_VALUE];
public firstCall() {
myBigThing[0] = "42";
}
public lastCall() {
myBigThing[1] = "Hello World!";
}
}
I call firstCall() right at the start of my program. I call
lastCall() right at the end of my program. All the time in between
the GC cannot use the memory taken by myBigThing because there is
still a live reference to it.
Have a very careful look at the size of any variables you have moved
from method to class level.
apparently duplicating the statement to declare the type of variable
within each method which needs it is better practice and better memory
management.
It is. The programmers who wrote the GC are better programmers than
any of us here. Let the GC do its work, don't try to second guess it.
rossum