Normally when you invert a sparse matrix the inverse is dense.
This imply to have enough memory to store the inverse, in your case the matrix is not so big for nowdays computers. In double precision (1 cell = 8 byte) you have
$$
31000 \times 31000 \times 8 \text{ byte } = 61504000000 \text{ byte }
\approx 7.7 \text{ Gygabyte }
$$
Another problem is the time invert a matrix, because it is a very heavy task.
For example Matlab inv describe the function so:
inv performs an LU decomposition of the input matrix (or an LDL decomposition if the input matrix is Hermitian). It then uses the results to form a linear system whose solution is the matrix inverse inv(X). For sparse inputs, inv(X) creates a sparse identity matrix and uses backslash, X\speye(size(X)).
Scipy linalg.inv recall the getry function of Lapack where:
Purpose
=======
DGETRI computes the inverse of a matrix using the LU factorization
computed by DGETRF.
This method inverts U and then computes inv(A) by solving the system
inv(A)*L = inv(U) for inv(A).
These for show the idea under the inversion functions, for the library UJMP I found problem to acces to the documentation. Note that solve the linaer system related to the action of the inverse is cheaper than calculate the inverse.
However if we wait the time for the inversion we will obtain, in general, a result not so good from to use in numerical calculation. For detail see this question.
For these reason you must valuate what are your goal with the inverse, and if you really need the explicit inverse of the application of the inverse.
BLAS & LAPACK
In your case you need sparse BLAS or LAPACK. For an example, not in java, see this nist link.
In this wikipedia page there are some java libraries, I cite some (note I do not use them):
MTJ supports sparse matrix storage but does not provide solvers for sparse matrices. Have a look at Sparse Eigensolvers for Java or consider implementing your own and letting us know about it (e.g. by using the ARPACK backend which comes with netlib-java).
GPU
Before use gpu I suggest to understand if you need the explicit inverse
However gpu not always give you speed up, but is depends by your specif problem. Without information, i.e. in general, you can think to use the gpu for the linear algebra (for example iterative solver). Normally gpu is bounded by memory transfer and it has got less ram of cpu.
UPDATE Memory Test
I replicate your test of creation of a matrix in octave. My pc has go 16 GB of ram plus 32 GB of hard disk space dedicate to ram. I use Octave 4.0 from Ubuntu 16.04 repository.
I launch the command
A=zeros(31000)
The ram used before the command 1.4 GB and at the end 8.6 GB, the measures come from monitor of system. So we can consider in line with prediction.
In the middle what happened?
In the middle there are one or two minutes of work (to be honest I did not use tic toc functions) by Octave. The memory usage varies during this time.
First the memory usage went to 9 GB quickly at as the same velocity it went to over the 16 GB of RAM and started to use the hard disc ram.
After a while the memory ram has decreased and started the moving from hard disc memory to normal ram.
At the end the memory value was correct.
Note that Octave memory error message is like this:
error: memory exhausted or requested size too large for range of Octave's index type -- trying to return to prompt
Thing that you do not see (according with the comment).
I am not sure at 100% but knowing Octave I have an idea. Octave, similar Matlab, has got a memory management by value this is a general approach that it uses. In documentation are explained some techniques that it use to improve performance.
The ram behavior can be compatible with this explanation: during the call of function zeros the matrix of $31000 \times 31000$ elements are created a first time (inside the normal ram). Copied, for some motivation, to the variable A, saved some in normal ram and the surplus in hard disk ram. After the first matrix (the first copy) is deleted and start the migration from the hard disk ram to normal ram, maybe with some cache techniques.
In my opinion you did not found a memory outbound (no messagge) but you were waiting the memory transfers. Obviously you have got a lot of ram memory, but How many free? (I think you need a check about this, remember that in my pc with less memory it runs).
Different the case for UJMP, according with your question:
But when I try to invert the matrix it cannot give any output also it cannot throw any error.
If you arrived to this point the matrix was already created and, I think, you are wait for the inversion task, that is very very heavy (normally never use in numerical analysis).