Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import static java.nio.file.StandardOpenOption.*; |
| 18 | import java.nio.file.*; |
| 19 | import java.io.*; |
| 20 | import java.util.*; |
| 21 | |
| 22 | public class Main { |
| 23 | private static final String TEMP_FILE_NAME_PREFIX = "oflimit"; |
| 24 | private static final String TEMP_FILE_NAME_SUFFIX = ".txt"; |
| 25 | |
| 26 | public static void main(String[] args) throws IOException { |
| 27 | |
| 28 | // Exhaust the number of open file descriptors. |
| 29 | List<File> files = new ArrayList<File>(); |
| 30 | List<OutputStream> streams = new ArrayList<OutputStream>(); |
| 31 | try { |
| 32 | for (int i = 0; ; i++) { |
| 33 | File file = createTempFile(); |
| 34 | files.add(file); |
| 35 | streams.add(Files.newOutputStream(file.toPath(), CREATE, APPEND)); |
| 36 | } |
| 37 | } catch (Throwable e) { |
| 38 | if (e.getMessage().contains("Too many open files")) { |
| 39 | System.out.println("Message includes \"Too many open files\""); |
| 40 | } else { |
Richard Uhler | 9169082 | 2018-04-09 10:27:24 +0100 | [diff] [blame] | 41 | System.out.println("Unexpected exception:"); |
| 42 | e.printStackTrace(); |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | // Now try to create a new thread. |
| 47 | try { |
| 48 | Thread thread = new Thread() { |
| 49 | public void run() { |
| 50 | System.out.println("thread run."); |
| 51 | } |
| 52 | }; |
| 53 | thread.start(); |
| 54 | thread.join(); |
| 55 | } catch (Throwable e) { |
Richard Uhler | a5c61bf | 2016-10-24 15:54:44 +0100 | [diff] [blame] | 56 | System.out.println(e.getMessage()); |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Richard Uhler | 1e01815 | 2018-01-02 09:05:51 +0000 | [diff] [blame] | 59 | for (int i = 0; i < streams.size(); i++) { |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 60 | streams.get(i).close(); |
Richard Uhler | 1e01815 | 2018-01-02 09:05:51 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | for (int i = 0; i < files.size(); i++) { |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 64 | files.get(i).delete(); |
| 65 | } |
| 66 | System.out.println("done."); |
| 67 | } |
| 68 | |
| 69 | private static File createTempFile() throws Exception { |
| 70 | try { |
| 71 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
| 72 | } catch (IOException e) { |
| 73 | System.setProperty("java.io.tmpdir", "/data/local/tmp"); |
| 74 | try { |
| 75 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
| 76 | } catch (IOException e2) { |
| 77 | System.setProperty("java.io.tmpdir", "/sdcard"); |
| 78 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |