Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i explicitly clear the byte[]

I am creating new byte arrays which are not being collected by GC and are living in memory and increasing the private bytes. The code below gets executed every 10 seconds. How do I explicitly clear the variable after I am done with it?

byte[] outputMessage = new byte[10000];
//Do some work here
like image 597
Kiran Avatar asked May 05 '11 04:05

Kiran


People also ask

Can you print [] byte?

You can simply iterate the byte array and print the byte using System. out. println() method.

What can I do with byte array?

You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory. For large amounts of binary data, it would be better to use a streaming data type if your language supports it.

What is offset in byte array?

In computer science, an offset within an array or other data structure object is an integer indicating the distance (displacement) between the beginning of the object and a given element or point, presumably within the same object.

What is byte array size?

The bytearray class is a mutable sequence of integers in the range of 0 to 256.


3 Answers

How do you know they are not being collected? The code you provide is fine, and it should become eligible for collection if you don't have any dangling references to it.

Explicitly, you can clear a reference by

outputMessage = null;

and you can hint the GC that it should do its job with this:

GC.Collect();

However, there's no guarantee that your object will get collected, and manually messing with the GC is usually not needed.

If you are adding a new event handler all the time, are you deleting any old ones? If not, then they are still keeping the reference for you.

EDIT: for clarity, and for new info by OP

like image 178
Amadan Avatar answered Nov 11 '22 11:11

Amadan


Ensure that you have no references to your array. Examine that you have not an assignment to another variable that keeps the array in memory.

Do you leave the focus of your outputMessage?
- If it is declared inside a method: Do you leave it or do you have some (intendet) endless loop and remain in it?
- If it is declared global inside a class object: Does your complete class remain in memory by a reference to this?

like image 20
Markus Avatar answered Nov 11 '22 13:11

Markus


It's hard to say for sure without seeing the context that thats used in. Unless you are keeping around references to each outputMessage they will get garbage collected eventually whenever the GC decides to run.

What were you looking at to see that the private bytes kept growing and would never shrink? Also were you only running with the debugger attached or were you running in release?

Do you really need to create a new array every 10 seconds? It might be faster to simply Array.Clear and reuse it.

like image 2
BrandonAGr Avatar answered Nov 11 '22 13:11

BrandonAGr