In Java you cannot free memory explicitly.
It depends on JVM Garbage Collector settings. There are number of parameters for GC behavior.
Some of them are different from one JVM implementation and version to another.
But, you do not need to care about it. When there is no reference to the object anymore, memory will be cleaned when GC decides that is a time for it.
In general for all variables inside the method references gone when method completed and memory can be cleaned by GC.
You can suggest GC to run like:
...
// just for testing
b = null;
System.gc();
But it is a hint anyway. GC may run or may not.
Sometime having line like you do
b = null;
is good not only for testing, but it is not a common case.
You may try to manipulate GC settings at JVM parameters to make GC more aggressive to avoid often Full GC runs.
There are bunch of resources on Internet as example for Oracle JVM:
Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide
keep in mind: different JVM may have different parameters for GC.