memory problems

J

jj

Hi



I have a very simple function that creates memory, do something with
it, and returns:

static void test(int k)
{
byte [] buff = new byte[k];

//do some stuff with buff[]

buff=null;

}

After a few calls to this function, it runs out memory
In C++ I would use delete at the end, and here in java I've been told
that GC takes care of it, but it seems that it does not
Am I doing something wrong? how can I free this temp memory after I
have used it?

many thanks
 
M

Mark Thornton

jj said:
Hi



I have a very simple function that creates memory, do something with
it, and returns:

static void test(int k)
{
byte [] buff = new byte[k];

//do some stuff with buff[]

buff=null;

}

After a few calls to this function, it runs out memory
In C++ I would use delete at the end, and here in java I've been told
that GC takes care of it, but it seems that it does not
It does. Whatever is going wrong is due to some aspect which does not
appear in the outline code you have provided. Possibly because you are
retaining a reference to the allocated memory somewhere other than in
the 'buff' variable.

Mark Thornton
 
M

Mark Space

jj said:
Am I doing something wrong?

Yes and no. It is not normally necessary to null out references to
release them.

static void test(int k)
{
byte [] buff = new byte[k];
//do some stuff with buff[]
}

Works fine.

how can I free this temp memory after I
have used it?


The GC does it for you. As mentioned already, the problem with memory
is not here. Look elsewhere in your code.
 
R

Roedy Green

I have a very simple function that creates memory, do something with
it, and returns:

static void test(int k)
{
byte [] buff = new byte[k];

//do some stuff with buff[]

buff=null;

}


Write yourself an SSCCE. You will discover a program like that above
fleshed out does NOT run out of memory, even if you leave out the
buff=null.

see http://mindprod.com/jgloss/sscce.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

Now for something completely different:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top