max_force
max_force

Reputation: 799

lifetime of variable in java

private void func()
{
  String str = "hi!";
}

this is my code.

suppose I called func() in the onCreate event. I want to the near exact time, if thats possible to be determined, when GC will free the memory used by str.

Actually str stores a password. and i want to free it as soon as I can. currently after using str I set its value to something irrelevant.

I heard GC is the only thing that can free memories? is that correct? is there any other way of freeing the memory of a variable.

Upvotes: 1

Views: 713

Answers (6)

user468687
user468687

Reputation:

You can call System.gc() but this is merely advisory if you look at the java docs. You can't force a garbage collection in java and in general it's not a good idea to try.

If you really need a solution for this, you'd need to write your own custom password reader/writer using either byte arrays or JNI, and of course be very careful the password never gets turned into an Object. Otherwise you've lost.

Upvotes: 1

AlexR
AlexR

Reputation: 115398

First, java will hold this string as long as it wants. Theoretically until the JVM terminates. So what? Who can connect to it and bring the value? And how?

But if you afraid very much you can call System.gc() that will probably trigger the GC to run and remove the value. Still no guarantee.

There is yet another trick. String contains its content is private field char[] value. You can overwrite it using reflection, so no one will know your password.

Upvotes: 2

PeterMmm
PeterMmm

Reputation: 24630

I don't know very well the Dalvik VM but in standard Java there is no way to freeup the memory from your example because the string "hi!" lives in the constant pool.

If you change to

String str = new String("hi!");

that gives a string on the heap and can be GC.

Upvotes: 1

The str is not the problem in your case, when you are declaring a string, it is allocated in string pool of your application. You should create an array of chars instead.

Regarding the GC, you don not have any assurance when will collect that memory.

Upvotes: 1

user207421
user207421

Reputation: 311046

I want to the near exact time, if thats possible to be determined, when GC will free the memory used by str.

Bad luck, you can't have it.

Solution: store it in a char[] instead of a String, and zero out all elements of the char[] when you are done with it.

Upvotes: 9

HJW
HJW

Reputation: 23443

Once the garbage collector is satisfied that there is no longer references to it, it becomes eligible for garbage collection.

However, because this is a String, i recall this literal will still be in the String pool, however its object reference will be destroyed by the GC.

You can't however influence the behavior of the GC.

In your case of the method, the variable goes out of scope each time the method exits, when it goes out of scope, it becomes eligible for GC, but whether GC decides to free it, we can't control or predict.

Upvotes: 2

Related Questions