C++程序  |  56行  |  1.38 KB

/**
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/wait.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>

int main(void) {
  struct rlimit rlim;
  size_t i;

  int result = getrlimit(RLIMIT_AS, &rlim);
  printf("%lu\n", rlim.rlim_cur);
  printf("%lu\n", rlim.rlim_max);

  rlim.rlim_cur = 384 * 1024 * 1024;
  result = setrlimit(RLIMIT_AS, &rlim);

  /*
   * Issue exists only in kitkat. It passes in
   * android M onwards. In failure scenario, device reboots.
   */
  size_t to_alloc = 1.6 * 1024 * 1024 * 1024;
  char *mem = (char *)malloc(to_alloc);

  printf("%p\n", mem);
  if (mem) {
    for (i = 0; i < to_alloc; i++) {
      mem[i] = 0;
    }
    printf("filled\n");
  }

  free(mem);
  return 0;
}