Question: I have a requirement to store user ids appending with numbers like:

If user id is "stackoverflow", then first 7 letters of id "stackov" is taken and if it not there in the DataBase then it should be stored as "stackov001".

Now if another user gives similar user id as "stackoverflowed" then its user id should be stored as "stackov002" because 

I tried this by using substring but i am not able to get "00". I am able to get "stackov1" "stackov2" but not "stackov001".

Can soemone please help on this.

Answer: 
The Salah reply is wrong at so many levels I would consider this outright harmful. 

First, the JVM will not trigger Stop The World GC and clean out heap just before dying with OutOfMemoryError. The reason for OOM is that the GC has failed to clean the memory, usually during tens and hundreds of runs prior the OOM.

Second, the recommended ratios are not actually recommended by JVM vendors. The best ratios are heavily application dependent, so without knowing what you are doing you are better off keeping the defaults than following this advice.
**First** you need to take a look at the following memory parameters, it may not prevent the `OutOfMemoryError`, but you should learn about them:

```
-Xmx2048m -> this param to set the max memory that the JVM can allocate
-Xms1024m -> the init memory that JVM will allocate on the start up
-XX:MaxPermSize=512M -> this for the max Permanent Generation memory

```

Also and you may want to check this parameters also, those parameters will speed up the `GC` activities on your Young space, so the collection will be better.

```
-XX:MaxNewSize=  -> this need to be 40% from your Xmx value
-XX:NewSize=614m -> this need to be 40% from your Xmx value

```

also you may tell you `JVM` what type of `GC` to use like (i think this type is already enabled in the newest `JVM` versions) :

```
-XX:+UseConcMarkSweepGC

```

**Second**

When an `OutOfMemoryError` occured, the `JVM` will trigger the `Stop World GC`, so it will collect your object and the un referenced object, that why the heap size is less than you `-Xmx` value.

Usually in this case, if you analyzed this heap using one of the analyzing tools, you will clearly find the big objects that filled your memory, that will led you to the code and it will be easy to fix it.